locked
When is Viewstate restored for programmatically generated controls? RRS feed

  • Question

  • User-1178453918 posted

    I have 2 dynamically-generated DropDownLists, List1 and List2. List2 has a dependency on List1, in that List2 is populated via a method which requires List1's SelectedValue.

    For some reason, List1's SelectedValue is not reloaded until after List1 has been added to the Page object (or some other container belonging to the Page object). Is there any way I can manually force the Viewstate to restore itself instead?

    Tuesday, August 16, 2011 3:05 PM

Answers

  • User541108374 posted

    Hi,

    dynamically added controls will get up to pace with the rest of the page life cycle as soon as possible and will then keep running in the same pace as other controls.

    List1's SelectedValue is not reloaded until after List1 has been added to the Page object

    You're adding List1 to the control collection again in the Init event of the page? Later on viewstate is restored, before the Page_Load. So if you don't have the adding in the Init event, put it there like described in this article: Adding a dynamic control to a placeholder control and wire up the event.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 16, 2011 3:15 PM
  • User-1177472553 posted

    the view state of a control would be loaded only on Page_Load event .. so people sugges to create dynamic controls only on page load ..

    please refer to page life cycle in asp.net

    http://msdn.microsoft.com/en-us/library/ms178472(VS.80).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 16, 2011 11:17 PM

All replies

  • User541108374 posted

    Hi,

    dynamically added controls will get up to pace with the rest of the page life cycle as soon as possible and will then keep running in the same pace as other controls.

    List1's SelectedValue is not reloaded until after List1 has been added to the Page object

    You're adding List1 to the control collection again in the Init event of the page? Later on viewstate is restored, before the Page_Load. So if you don't have the adding in the Init event, put it there like described in this article: Adding a dynamic control to a placeholder control and wire up the event.

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 16, 2011 3:15 PM
  • User-1177472553 posted

    the view state of a control would be loaded only on Page_Load event .. so people sugges to create dynamic controls only on page load ..

    please refer to page life cycle in asp.net

    http://msdn.microsoft.com/en-us/library/ms178472(VS.80).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 16, 2011 11:17 PM