今天有用到动态生成控件:
protected void Page_PreRender(object sender, EventArgs e)
{
BoundControl();
}
{
List<OfferQuestion> offerQuestion = new List<OfferQuestion>();
if (Request.QueryString["OfferId"] != null && Request.QueryString["UserId"] != null)
{
offerQuestion = BLLOffer.GetQuestionForInfluencer(Request.QueryString["OfferId"].ToString(), Request.QueryString["UserId"].ToString(), -1); ;
}
if (Request.QueryString["OfferId"] != null && Request.QueryString["LanguageId"] != null)
{
offerQuestion = BLLOffer.GetQuestionForInfluencer(Request.QueryString["OfferId"].ToString(), "", Convert.ToInt32(Request.QueryString["LanguageId"]));
}
if (offerQuestion != null)
{
//Create a table for question.
Table table = new Table();
table.CellPadding = 4;
table.CellSpacing = 0;
table.Width = Unit.Percentage(98);
table.Attributes.Add("align", "center");
for (int i = 0; i < offerQuestion.Count; i++)
{
//Create 2 rows, one is the title, another is the context
TableRow tableRow1=new TableRow();
TableRow tableRow2 = new TableRow();
//Every row will has two column.
TableCell tableCell=new TableCell();
TableCell tableCell2 = new TableCell();
tableCell.VerticalAlign=VerticalAlign.Top;
tableCell.Width = 250;
//Create the label for title.
Label lb = new Label();
lb.ID = "_label" + offerQuestion[i].Question;
lb.Text = offerQuestion[i].Question + ":";
tableCell.Controls.Add(lb);
if (offerQuestion[i].Required)
{
Label lbValidation = new Label();
lbValidation.ID = "_validation" + i.ToString();
lbValidation.Text = "*";
lbValidation.ForeColor = System.Drawing.Color.Red;
tableCell.Controls.Add(lbValidation);
}
if (offerQuestion[i].QuestionType.Trim() == "TextBox")
{
TextBox tb = new TextBox();
tb.ID = "_" + offerQuestion[i].Question;
tb.Width = 150;
tb.Text = offerQuestion[i].PreDefinedAnswers;
//tb.Enabled = false;
tableCell2.Controls.Add(tb);
}
else
{
DropDownList ddl = new DropDownList();
ddl.ID = "_" + offerQuestion[i].Question;
ddl.Width = 150;
if (offerQuestion[i].Question == "Country")
{
List<Country> Country = BLLMetadata.GetCountiesBySubRegion(null); //Bind drlCountry for country
ddl.DataSource = Country;
ddl.DataTextField = "CountryName";
ddl.DataValueField = "CountryID";
ddl.DataBind();
ddl.SelectedIndex = 0;
ddl.Items.Insert(0, new ListItem("<Select>", "-1"));
//ddl.Enabled = false;
tableCell2.Attributes.Add("style", "padding-left:18px");
}
tableCell2.Controls.Add(ddl);
}
tableRow1.Cells.Add(tableCell);
tableRow2.Cells.Add(tableCell2);
tableCell = new TableCell();
tableCell2 = new TableCell();
if (i < offerQuestion.Count - 1)
{
tableCell = new TableCell();
lb = new Label();
lb.ID = "_label" + offerQuestion[i++].Question;
lb.Text = offerQuestion[i].Question + ":";
tableCell.Width = 200;
tableCell.Controls.Add(lb);
if (offerQuestion[i].Required)
{
Label lbValidation = new Label();
lbValidation.ID = "_validation" + i.ToString();
lbValidation.Text = "*";
lbValidation.ForeColor = System.Drawing.Color.Red;
tableCell.Controls.Add(lbValidation);
}
if (offerQuestion[i].QuestionType.Trim() == "TextBox")
{
TextBox tb = new TextBox();
tb.ID = "_" + offerQuestion[i].Question;
tb.Width = 150;
tb.Text = offerQuestion[i].PreDefinedAnswers;
//tb.Enabled = false;
tableCell2.Controls.Add(tb);
}
else
{
DropDownList ddl = new DropDownList();
ddl.ID = "_" + offerQuestion[i].Question;
ddl.Width = 150;
if (offerQuestion[i].Question == "Country")
{
List<Country> Country = BLLMetadata.GetCountiesBySubRegion(null); //Bind drlCountry for country
ddl.DataSource = Country;
ddl.DataTextField = "CountryName";
ddl.DataValueField = "CountryID";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("<Select>", "-1"));
ddl.SelectedIndex = 0;
tableCell2.Attributes.Add("style", "padding-left:18px");
}
tableCell2.Controls.Add(ddl);
}
tableRow1.Cells.Add(tableCell);
tableRow2.Cells.Add(tableCell2);
}
table.Rows.Add(tableRow1);
table.Rows.Add(tableRow2);
}
this.PlaceHolder1.Controls.Add(table);
}
}
浙公网安备 33010602011771号