如何在asp.net中删除动态创建的文本框

时间:2014-12-02 06:59:39

标签: c# asp.net

我已经创建了添加文本框的代码,但我不知道要删除添加的文本框。

分享我如何删除文本框;

counter ++; 
TextBox tb = new TextBox(); 
tb.ID = "TextBox" +counter; 
LiteralControl lineBreak = new LiteralControl(); 
PlaceHolder1.Controls.Add(tb); PlaceHolder1.Controls.Add(lineBreak);   
controlIdList.Add(tb.ID); 
ViewState["controlIdList"] = controlIdList;

1 个答案:

答案 0 :(得分:0)

您可以从占位符中移除文本框,如下所示:

    protected void Remove(object sender, EventArgs e)
    {
        foreach (Control control in PlaceHolder1.Controls)
        {
            //Here you need to take ID from ViewState["controlIdList"]
            if (control.ID == "TakeIDFromControlListsID")
            {
                Controls.Remove(control);
            }
        }
     }