Wednesday, 11 September 2013

ASP.NET Dynamically load controls based on ViewState

ASP.NET Dynamically load controls based on ViewState

I have a UserControl which dynamically creates several TextBoxes. The
number of TextBoxes is dependent on user selection. I chose to store the
IDs of each TextBox in the UserControl's ViewState object so that they can
be re-created consistently across each postback.
If I re-create the dynamic controls during Page_Init (as is recommended)
then the UserControls's ViewState has not yet been populated.
If I re-create the dynamic controls during Page_Load then their postback
values are not rehydrated until after this event, so I can't access their
correct values until later in the page lifecycle which is causing
problems.
Is there a better approach to this?

No comments:

Post a Comment