Search code examples
asp.netpage-lifecycle

What is the last stage to attach viewstate Data to a dynamic control?


I have an Ascx control which I reload every Page_load using Loadcontrol.

This Ascx has a Button And Label

The Label initially contains "aaa".

When I click the button the page reloads and recreates the ascx.

The good thing is that the "aaa" is kept - although it's a new instance of the ascx.

I was wondering to my self why ? and I've answered to myself that's its because the ID of the ASCX which is the same so he is attaching the viewstate from the last postback to the current instance.

Question

What is the latest page event which I can create the control so that it will be able to attach the viewstate (from the last postback)to the new generated control ?


Solution

  • Following are the events occur during ASP.NET Page Life Cycle:

    1)Page_PreInit
    2)Page_Init
    3)Page_InitComplete
    4)Page_PreLoad
    5)Page_Load
    6)Control Events
    7)Page_LoadComplete
    8)Page_PreRender
    9)SaveViewState
    10)Page_Render
    11)Page_Unload
    

    Among above events Page_Render is the only event which is raised by page. So we can't write code for this event.