0

ASP.NET C#.

Inside UpdatePanel we have TextBox with OnTextChanged="text_changed" method and Panel.

if number 3 was typed at textbox, 3 textboxes below will appear inside Panel with different IDs.

However when button outside updatepanel clicks, dynamically created textboxes not found error occured.

How to get values of dynamically created textboxes?

Creating textbox:

protected void text_changed(Object sender, EventArgs e)
        {
           int n = Int32.Parse(TextBox6.Text);
           Table table = new Table();

           for (int i = 0; i < n; i++)
            { 
                TableRow trow = new TableRow();             
                table.Rows.Add(trow);

                TableCell tcell = new TableCell();
                tcell.Text = (i + 1).ToString();   
                TextBox tb = new TextBox();
                tb.ID = "TB" + i.ToString();
                tcell.Controls.Add(tb);
                trow.Cells.Add(tcell);
            }
            Panel1.Controls.Add(table);

ButtonClick //get values from created textboxes: int n = Int32.Parse(TextBox6.Text);

        for (int i = 0; i < n; i++) 
        {
            string title = ((TextBox)UpdatePanel1.FindControl("Panel1").FindControl("TB" + i.ToString())).Text;   //here null pointer exception..            
        }
Nurlan
  • 2,860
  • 19
  • 47
  • 64

1 Answers1

1

where are you generating your textboxes? if you're creating them in text_changed event, then on the next post back your going to run into pagelife cycle issues. you'd need to cache the fact that you created them, and recreate them in the OnInit phase of the page.

Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
  • edited post by adding necessary code. If recreate them in OnInit then textboxes do not appear in page.. – Nurlan May 28 '13 at 10:11