Click here to Skip to main content
15,898,732 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I tried to create TextBox on a WebForm Dynamically but the textboxes is not displayed on the form after running. I tested with the use of breakpoint but there is no error . The same code works fine for creation of Checkboxes. Pls Help ...

Here is the code:
C#
protected void btnItem_Click(object sender, EventArgs e)
       {
           Table tblTxtBox = new Table();

           for (int i = 0; i < 5; i++)
           {

               TableCell tblcell = new TableCell();

               TableRow tblrow = new TableRow();

               TextBox txtBox = new TextBox();

               txtBox.ID = "txtBox" + i;

               tblcell.Controls.Add(txtBox);

               tblrow.Cells.Add(tblcell);

               tblTxtBox.Rows.Add(tblrow);

           }

           Panel1.Controls.Add(tblTxtBox);
       }
Posted
Updated 14-Dec-12 18:14pm
v2
Comments
AmitGajjar 15-Dec-12 0:15am    
After postback your control will be lost. so you need to regenerate all the dynamic controls on page_load or page_init.

 
Share this answer
 
This will never be reliable. The controls will not work with viewstate, nor will they survive a page refresh. Your best bet with dynamic controls is no postbacks, and to handle sending data back using AJAX.
 
Share this answer
 
updated:
rahkan's answer is more accurate. Read his answer.

at your page load event, add this:
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
                
    }
    AddTextBox();
}

void AddTextBox()
{
    Table tblTxtBox = new Table();
    for (int i = 0; i < 5; i++)
    {
        TableCell tblcell = new TableCell();
        TableRow tblrow = new TableRow();
        TextBox txtBox = new TextBox();
        txtBox.ID = "txtBox" + i;
        tblcell.Controls.Add(txtBox);
        tblrow.Cells.Add(tblcell);
        tblTxtBox.Rows.Add(tblrow);
    }
    Panel1.Controls.Add(tblTxtBox);
}
 
Share this answer
 
v2
Comments
[no name] 15-Dec-12 0:58am    
4.5
When dynamically creating controls you need to make sure that you are reloading the controls during any postback event. The controls need to be loaded with the same ID and in the same order as they were created. What @adriancs posted should work as long as the user has only clicked once on the button that adds the textboxes. What I generally do in a situation like this is save the count of the dynamically added controls using a session variable, then during a postback I can reload the controls using the count from the session variable.

C#
public int TextBoxCount
{
    get
    {
        object temp = Session["TextBoxCount"];
        return temp == null ? 0 : (int)temp;
    }
    set
    {
        Session["TextBoxCount"] = value;
    }
}


C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
                
    }
    AddTextBox();
}
 
void AddTextBox()
{
    Table tblTxtBox = new Table();
    
    for (int i = 0; i < TextBoxCount; i++)
    {
        TableCell tblcell = new TableCell();
        TableRow tblrow = new TableRow();
        TextBox txtBox = new TextBox();
        txtBox.ID = "txtBox" + i;
        tblcell.Controls.Add(txtBox);
        tblrow.Cells.Add(tblcell);
        tblTxtBox.Rows.Add(tblrow);
    }

    Panel1.Controls.Add(tblTxtBox);
}


C#
protected void btnItem_Click(object sender, EventArgs e)
{
    Table tblTxtBox = new Table();
    int start = TextBoxCount;
    TextBoxCount += 5;

    for (int i = start; i < TextBoxCount; i++)
    {
 
        TableCell tblcell = new TableCell(); 
        TableRow tblrow = new TableRow(); 
        TextBox txtBox = new TextBox();
 
        txtBox.ID = "txtBox" + i;
            
        tblcell.Controls.Add(txtBox);          
        tblrow.Cells.Add(tblcell); 
        tblTxtBox.Rows.Add(tblrow); 
    }

    Panel1.Controls.Add(tblTxtBox);
}


That may need a little tweaking as I'm just working from memory right now, but hopefully it helps some
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900