0
votes

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..            
        }
1
Have you tried using jQuery with something like $("#generaredID").html(); - ianaldo21
Add your textbox creation code to your question? - Veer
edited post by adding necessary code - Nurlan

1 Answers

1
votes

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.