点button添加label
前台代码
<asp:Label ID="Label1" runat="server"></asp:Label> <asp:Label ID="Label2" runat="server"></asp:Label> <br /> <asp:Panel ID="Panel1" runat="server"> <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="添加label" /> </asp:Panel>
后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ViewState["max"] = 1; } }
protected void Button4_Click(object sender, EventArgs e) { ////循环添加label并将label的text的值循环加入list中 List<Label> demo = new List<Label>(); int max = Convert.ToInt32(ViewState["max"]); for (int i = 0; i < max; i++) { Label l = new Label(); l.ID = "lbl" + i; l.Text = "label"; Panel1.Controls.Add(l); demo.Add(l); } ViewState["max"] = max + 1; ////循环读取list的demo里面所有的值给strValue string strValue = ""; for (int i = 0; i < demo.Count; i++) { strValue += demo[i].Text; } Label2.Text = strValue;//的到的值给 Label2.Text Label1.Text = Convert.ToString(max);//添加label的个数 }