怎么用ASP.NET服务端技术向客户端动态添加元素(文本框,按钮,label标签...) 

解决方案 »

  1.   

    <form id="form1" runat="server">
    TextBox t = new TextBox();
    form1.Controls.Add(t);
      

  2.   

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:LinkButton ID="lbtnAdd" runat="server" Text="add" OnClick="lbtnAdd_Click" />
        <hr />
        <asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
        </form>
    </body>
    </html>protected void Page_Load(object sender, EventArgs e)
        {
            object count = ViewState["count"] ?? "0";        int index;
            int.TryParse(count.ToString(), out index);        TextBox txt;
            LiteralControl litc;        for (int i = 0; i < index; i++)
            {
                txt = new TextBox();
                txt.ID = string.Format("txt{0}", i);
                txt.Text = (i + 1).ToString();
                ph.Controls.Add(txt);            litc = new LiteralControl("<br />");
                ph.Controls.Add(litc);
            }    }
        protected void lbtnAdd_Click(object sender, EventArgs e)
        {
            object count = ViewState["count"] ?? "0";        int index;
            int.TryParse(count.ToString(), out index);        TextBox txt = new TextBox();
            txt.ID = string.Format("txt{0}", index);
            txt.Text = (index + 1).ToString();        ph.Controls.Add(txt);        ViewState["count"] = index + 1;
        }