循环添加控件
<div id="div1" runat="server"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="btn1" runat="server" OnClick="btn1_Click" style="margin-left: 39px" Text="创建文本框" /> <asp:TextBox ID="TextBox2" runat="server" style="margin-left: 149px">123</asp:TextBox> <br /> <asp:Panel ID="panel1" runat="server" Width="760px"> </asp:Panel> <br /> <asp:Button ID="Button1" runat="server" style="margin-left: 163px" Text="文本框的个数" OnClick="Button1_Click" /> </div>
protected void Page_Load(object sender, EventArgs e) { int i = 0; try { i = Int16.Parse(TextBox1.Text); } catch { } for (int o = 0; o < i; o++) { TextBox a = new TextBox(); a.Text = o.ToString(); panel1.Controls.Add(a); } } protected void btn1_Click(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { int i = 0; foreach(Control a in div1.Controls) { if(a is TextBox) { i++; } } Response.Write(i); } }