为他们动态创建文本框,复选框和Lbael?

时间:2013-06-03 12:57:52

标签: c# asp.net

对于每个按钮,单击我想动态创建文本框及其相应的标签。请参阅下面的代码。

protected void btnAddField_click( Object sender, EventArgs e ) 
{
CheckBox chkNewField = new CheckBox();
chkNewField.ID      = "chkNewField"+ FieldCount.ToString();
chkNewField.Checked = true;

Label LblNewLabel   = new Label();
LblNewLabel.ID = "lblNewLabel" + FieldCount.ToString();
LblNewLabel.Text    = "New Lable";

TextBox TxtNewLabel = new TextBox();
TxtNewLabel.ID = "TxtNewLabel" + FieldCount.ToString();

Label LblNewValue   = new Label();
LblNewValue.ID = "lblNewValue" + FieldCount.ToString();
LblNewValue.Text = "New Value";

TextBox TxtNewValue = new TextBox();
TxtNewValue.ID = "TxtNewValue" + FieldCount.ToString();

HtmlTableRow  tRow   = new HtmlTableRow ();

HtmlTableCell tCell1 = new HtmlTableCell("th");
HtmlTableCell tCell2 = new HtmlTableCell("th");
tCell2.Attributes.Add( "class", "medium" );
HtmlTableCell tCell3 = new HtmlTableCell("th");
tCell3.Attributes.Add( "class", "medium" );
HtmlTableCell tCell4 = new HtmlTableCell();
HtmlTableCell tCell5 = new HtmlTableCell("th");
tCell5.Attributes.Add( "class", "medium" );
HtmlTableCell tCell6 = new HtmlTableCell("th");
tCell6.Attributes.Add( "class", "medium" );

tCell1.Controls.Add(chkNewField);
tCell2.Controls.Add(LblNewLabel);
tCell3.Controls.Add(TxtNewLabel);
tCell4.Controls.Add( new LiteralControl( "" ) );
tCell5.Controls.Add( LblNewValue );
tCell6.Controls.Add( TxtNewValue );

tRow.Cells.Add(tCell1);
tRow.Cells.Add(tCell2);
tRow.Cells.Add(tCell3);
tRow.Cells.Add(tCell4);
tRow.Cells.Add(tCell5);
tRow.Cells.Add(tCell6);

tblPropertyCustom.Rows.Add(tRow );
FieldCount++;

}

它只显示一行标签和字段。在下一次单击时,行值将替换为下一个计数的标签和字段。请帮帮我(我只能看到一行)。

0 个答案:

没有答案