Reputation: 1
OK, got pretty far on this one, but need an assist from someone with superior ASP skills. I'm using code behind to populate an ASP table with the results of a SQL query. The read-only values are stored in the .text
of some of the table cells, while read-write values are stored in .text
of textbox controls (dynamically created and added to table cells.)
This works fine on the first load. When the page reloads with a different query (for example: a user selects a different column to order by,) the table cell values repopulate correctly, while the textbox values remain unchanged. Throwing in a table.rows.clear()
prior to the query does not seem to fix this.
More info:
textbox.text
values using table.findcontrol()
. When tied to a button, this method works to spec (which indicates findcontrol is able to find/update the textboxes,) though all affected textboxes remain the blank if the page is reloaded. If placed in the page load, the method does nothing (textboxes retain their former values.) In debug mode, findcontrol pulls a value when used on the button, but comes up null when added to pageload. I have done this with table.rows.clear()
commented and uncommented.oninit
. This doesn't seem to make any appreciable difference.Upvotes: 0
Views: 1418
Reputation: 55
In order for dynamic controls to retain their values you need to:
a.) Ensure Viewstate is enabled for the page. b.) Recreate the controls on every page load ensuring you create the controls with the same IDs as were given to the controls originally. Also you need to do this before the viewstate is loaded (preferbly in the Onit or PreInit method. See the page life cycle here: http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx
Upvotes: 0
Reputation: 13419
Dynamic controls must be added on each page request, preferably during the Init
event. It sounds like you are not recreating them on time.
Add them on every single page request and, as part of the page life cycle, they will receive their values from the viewstate
Upvotes: 2