Question

I have a server-side table control. Since I don't know how many rows and columns it will have beforehand, I build the contents of this table in my pages Form_Load event handler.

That works well. But I'm populating that same table with textboxes, and I need to be able to read those textboxes if a submit button is clicked.

As it stands, it appears the table contents are cleared upon postback.

What is the best way to dynamically create textboxes in a table, and then read the values of those textboxes when the page is posted back?

Was it helpful?

Solution

If you want to persist the values on postback you will need to recreate the table control hierarchy on the Page_Init event. This usually means that you will need to rebind your table control to a data structure coming from the database.

Once the control hierarchy is in place, ASP.NET will load any data from the ViewState. This data can be in fact data that was previously set from the server on the textboxes. This step will ensure that these values are preserved on the control.

In the next step ASP.NET will process the POST data, and here basically will update the table textboxes with the input from the client (browser). Any values set from the ViewState will (most probably) be overwritten.

All this happens before Page_Load, so at this stage you should be able to access the client textboxes values.

Check out the ASP.NET Page Life Cycle, especially this image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top