内容摘要 -
全文 -
asp.net(c#)中动态创建添加控件的一个例子
一个例子,动态添加控件的
<%@ Page Language="C#" %> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) ViewState["Count"]=0; else for(int i=1;i<=(int)ViewState["Count"];i++) { addproduct(i.ToString()); } } void addproduct(string num) { LiteralControl mylabel; TextBox mybox = new TextBox(); mylabel = new LiteralControl(); mylabel.Text = "<p>" + "产品:" + num; PlaceHolder1.Controls.Add(mylabel); mybox = new TextBox(); mybox.ID = "txtProduct" + num; PlaceHolder1.Controls.Add(mybox); } protected void add_btn(object sender, EventArgs e) { int count =(int)ViewState["Count"]; count++; ViewState["Count"] = count; addproduct(ViewState["Count"].ToString()); } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>动态添加控件</title> </head> <body style="font-size: 12px;"> <form id="form1" runat="server"> <div> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> <br /> <asp:Button ID="Button1" runat="server" Text="增加" OnClick="add_btn" /> </form> </body> </html>
|