views:

397

answers:

3

Hello Guys! My problem is that I have an ASPX page which contains an ASP:Table. The rows for the table is added dynamically on Page_Load. One column in the table contains TextBoxes, BUT when I type something on a TextBox and cause a postback, I am unable to find the value just entered. And above that the table is not displayed after the postback.

Can anyone help me please? I want to keep the table viewstate with the modified textbox values, so that when i post back to server, I can intercept these new values.

Thanks in advance, Kevin

+1  A: 

You will need to ensure you re-create the textboxes with the same IDs, and then you should be able to find the value. For example, if you are using TextChanged events, these will fire so-long as the textboxes are re-created each time and have the same IDs.

Codesleuth
A: 

Are the rows of the table (or the table itself) added at every Page_Load? They should be, if you want them to work correctly between postbacks.

Edit: With the same IDs, as another answer pointed out.

Anders Fjeldstad
So if i got it right, i must re=add the table rows with the same IDs, but what if the rows do not have an ID. In fact, only the textboxes have an ID. Suppose I change the value of textbox from 2 to 20, and then cause a postback, if I recreate the table rows with textbox after the postback, how will it retain the value 20?
Suraj
As long as the actual table itself has the same ID, its rows should get the right IDs as well automatically. And your textboxes need to have the same ID between postbacks also. Do you have a special reason for adding the table dynamically instead of using a GridView and just changing the contents of its data source?
Anders Fjeldstad
A: 

Dynamic controls need to be added during the Init event (and on every load) if you want ViewState to track anything, because ViewState is restored before Page_Load.

However, TextBox values are found in the post data, not the Viewstate. This is a common misconception. The same rule applies though.

Bryan