protected void createCtrl()
    {
        HtmlGenericControl textBox = new HtmlGenericControl("input");
        textBox.Attributes.Add("Id", "Tinct");
        textBox.Attributes.Add("Name", "Tinct");
        textBox.Attributes.Add("value", "hi");
        textBox.Style.Add("width", "44px");
        this.Page.Controls.Add(textBox);
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        createCtrl();
    }
如果点击Button1时就创建一个控件!但是无法创建多个textBox?点击第二次时,原先的控件被覆盖掉了!在页面上只能看到一控件!如何改才能创建多个textBox控件尼??谢谢!!

解决方案 »

  1.   

    每次动态创建之后,需要PostBack在Page_Load里面重新动态创建,以还原控件树。    protected void createCtrl()
        {
            HtmlGenericControl textBox = new HtmlGenericControl("input");
            textBox.Attributes.Add("Id", "Tinct");
            textBox.Attributes.Add("Name", "Tinct");
            textBox.Attributes.Add("value", "hi");
            textBox.Style.Add("width", "44px");
            this.Page.Controls.Add(textBox);
           ViewState["create"] = true;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            createCtrl();
        } Page_Load()
    {
        if(ViewState["create"] != null && Convert.ToBoolean(ViewState["create"]))
        {
            createCtrl();
        }
    }
    第二个动态控件的ID要不同于第一个。