Controls.remove() method not working in asp.net

Posted by user279521 on Stack Overflow See other posts from Stack Overflow or by user279521
Published on 2010-04-26T18:29:29Z Indexed on 2010/04/26 18:33 UTC
Read the original article Hit count: 269

Filed under:
|

I have a web app where the user can create dynamic textboxes at run time. When the user clicks SUBMIT, the form sends data to the database and I want remove the dynamic controls.

The controls are created in the following code:

Table tb = new Table(); 
tb.ID = "tbl"; 

for (i = 0; i < myCount; i += 1) 
{ 
    TableRow tr = new TableRow(); 

    TextBox txtEmplName = new TextBox(); 
    TextBox txtEmplEmail = new TextBox(); 
    TextBox txtEmplPhone = new TextBox(); 
    TextBox txtEmplPosition = new TextBox(); 
    TextBox txtEmplOfficeID = new TextBox(); 

    txtEmplName.ID = "txtEmplName" + i.ToString(); 
    txtEmplEmail.ID = "txtEmplEmail" + i.ToString(); 
    txtEmplPhone.ID = "txtEmplPhone" + i.ToString(); 
    txtEmplPosition.ID = "txtEmplPosition" + i.ToString(); 
    txtEmplOfficeID.ID = "txtEmplOfficeID" + i.ToString(); 

    tr.Cells.Add(tc); 
    tb.Rows.Add(tr); 
} 
Panel1.Controls.Add(tb); 

The Remove section of the code is:

Table t = (Table)Page.FindControl("Panel1").FindControl("tbl"); 
foreach (TableRow tr in t.Rows) 
{ 
    for (i = 1; i < myCount; i += 1) 
    { 
        string txtEmplName = "txtEmplName" + i; 
        tr.Controls.Remove(t.FindControl(txtEmplName)); 
        string txtEmplEmail = "txtEmplEmail" + i; 
        tr.Controls.Remove(t.FindControl(txtEmplEmail)); 
        string txtEmplPhone = "txtEmplPhone" + i; 
        tr.Controls.Remove(t.FindControl(txtEmplPhone));
        string txtEmplPosition = "txtEmplPosition" + i; 
        tr.Controls.Remove(t.FindControl(txtEmplPosition)); 
        string txtEmplOfficeID = "txtEmplOfficeID" + i; 
        tr.Controls.Remove(t.FindControl(txtEmplOfficeID)); 

    } 
} 

However, the textboxes are still visible.

Any ideas?

© Stack Overflow or respective owner

Related posts about c#3.5

Related posts about ASP.NET