Click here to Skip to main content
15,036,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,Everyone
I have created textbox's dynamically. Now i want to read the textbox's values which are created dynamically. following is the code which i have written to create the textbox's dynamically:

Eveytime we don't know how many fields to be created,hence i used i used count variable to create the textbox's equal to the count.
for (int i = 0; i<;count; i++)
            {
                TextBox tb = new TextBox();
                tb.ID = "tb" + i;
                tb.EnableViewState = true;
                Panel1.Controls.Add(tb);
             }

from the above code, ID's for textbox's created as tb0,tb1,tb2 and so on...
while saving i don't know the exact number of textbox's now how can i read the textbox's values.

I used the following code to get the ID's of my textbox's in my submit button event but unable to get it:
for (int i = 0; i < Convert.ToInt32(ViewState["count"].ToString()); i++)
  {
     TextBox tb = new TextBox();
     tb = (TextBox)Panel1.FindControl("tb" + i);
  }

The values in my textbox's are comma seprated,how do i get this values individually.
Posted

1 solution

Loop through all the controls present in your panel. If there are controls other than textbox then check for control type before accessing/using it. In case there might be some other textbox too then look for the ID's too. This way you have access to all the available textbox control and you can look at the values one-by-one.

BTW, what issue you are facing with current codebase. If you really want the count and move with your way then you can use Session to store the value and then use it later.
   
Comments
Ulhas Chaudhary 10-Dec-10 2:25am
   
the user would enter the values at run time how can i read those values.
Even if i loop through all the controls how can i read the ID's of multiple textbox's at a time.
Sandeep Mewara 10-Dec-10 3:11am
   
Well, when you will loop through all controls, just use ID property to get ID.

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