ASP.NET: Viewstate and programmatically adding user controls

前端 未结 1 646
攒了一身酷
攒了一身酷 2021-01-12 06:05

When programmatically adding user controls using LoadControl(string path), when, in the user control\'s page life cycle, does it initialize its sub-controls with its viewsta

相关标签:
1条回答
  • 2021-01-12 06:42

    ViewState is loaded before the Page_Load event. If you want your control to work with ViewState, you need to load it and add it to the page before that event — usually on PreInit.

    The life cycle reference is here:
    http://msdn.microsoft.com/en-us/library/ms178472.aspx?ppud=4

    Read the description for the Pre Load event, which immediately precedes Page Load:

    Use this event if you need to perform processing on your page or control before the Load event.

    Before the Page instance raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.

    Thus by Pre Load time it's already too late. Also, the description for the PreInit event specifically mentions that it's the place to "create or re-create dynamic controls."

    0 讨论(0)
提交回复
热议问题