日期:2014-05-16 浏览次数:20461 次
//前台显示代码
<body>
<form id="form1" runat="server">
<div runat="server">
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"
/>
</div>
</form>
</body>
//后台代码
public partial class Default4 : System.Web.UI.Page
{
static int i = 1;
static List<Panel> palList = new List<Panel>();
protected void Page_Load(object sender, EventArgs e)
{
RestoreTextBox();
}
protected void Button1_Click(object sender, EventArgs e)
{
Panel a = new Panel();
a.ID = "div" + i;
TextBox tx1 = new TextBox();
tx1.ID = "txta" + i;
tx1.Text = "txta" + i;
a.Controls.Add(tx1);
TextBox tx2 = new TextBox();
tx2.ID = "txtb" + i;
tx2.Text = "txtb" + i;
a.Controls.Add(tx2);
palList.Add(a);
Panel1.Controls.Add(a);
i++;
}
public void RestoreTextBox() {
foreach (Panel tx in palList) {
if (tx != null) {
Panel1.Controls.Add(tx);
}
}
}
}