17,741
社区成员
发帖
与我相关
我的任务
分享
<div id="ss" runat="server">
</div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
{
TextBox tb = new TextBox();
this.ss.Controls.Add(tb);
}
protected void Page_Load(object sender, EventArgs e)
{
// Rebuild TextBoxs
foreach (var id in dynamicTextBoxIds)
{
TextBox text = new TextBox();
text.ID = id;
text.Text = Request.Form[id];
myDiv.Controls.Add(text);
myDiv.Controls.Add(new LiteralControl("<br>"));
}
}
private List<string> dynamicTextBoxIds
{
set { ViewState["DynamicTextBoxIds"] = value; }
get { return (List<string>)ViewState["DynamicTextBoxIds"] ?? new List<string>(); }
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox text = new TextBox();
text.ID = "TextBox_" + dynamicTextBoxIds.Count;
List<string> textIds = dynamicTextBoxIds;
textIds.Add(text.ID);
// Save to ViewState
dynamicTextBoxIds = textIds;
myDiv.Controls.Add(text);
myDiv.Controls.Add(new LiteralControl("<br>"));
}