The page goes through the following phases:
1. The page first retrieves the posted data from the QueryString or Form collection of the Request object.
2. The page then checks whether the posted Data collection (the NameValueCollection Form or QueryString) contains an item with the key __CALLBACKID. If it does, it sets its IsCallbackBoolean property to true to signal that the page has been posted back to the server through theASP.NET client callback mechanism.
3. PreInit: The page takes the following actions in the PreInit phase of its life cycle:
- Calls its OnPreInit method to raise the PreInit event.
- Initializes the theme by using the contents of the App_Themes directory to dynamicallyimplement a class of type PageTheme, compiles the class, creates an instance of thecompiled class, and assigns the instance to its PageTheme property.
- Applies the master page.
- Recursively initializes the controls in its Controls collection. This initializationincludes setting the properties of these controls such as Page, ID, NamingContainer,and so on.
- Recursively applies these controls' skins.
- Calls its own OnInit method to raise its own Init event and then recursively calls thechild control’s OnInit methods to raise their Init events.
- Calls its own TrackViewState to start its own view state tracking and then recursivelycalls the child controls' TrackViewState methods to start their view state tracking.
6. Load Control State (postback only): The page recursively calls the LoadControlState method of those controls in its Controls collection that have called the RegisterRequiresControlStatemethod of the page class to express interest in using their control states.
7. Load View State (postback only): The page first calls its own LoadViewState method and then recursively calls the LoadViewState method of the controls in its Controls collection to allow them to load their saved view states.
8. Load Post Data (postback only - first try): The page calls the LoadPostData method of the controls that implement the IPostBackDataHandler interface and passes the posted data into it.The LoadPostData method of each control must access the posted data and update the respective property of the control accordingly. For example, the LoadPostData method of the TextBoxcontrol assigns the new value of the text box to the Text property of the TextBox control.
9. PreLoad: The page calls its OnPreLoad method to raise the PreLoad event. This event signals the beginning of the load phase of the page life cycle.
10. Load: The page first calls its own OnLoad method to raise its own Load event and then recursively calls the OnLoad methods of the controls in its Controls collection to raise their Load events. Page developers may register callbacks for the Load event, where they may programmatically add child controls to the Controls collection of the page.
11. Load Post Data (postback only second try): The page calls the LoadPostData method of those controls that were programmatically added to its Controls collection in the Load phase if they implement the IPostBackDataHandler interface.
12. Raise Post Data Changed Event (postback only): The page calls the RaisePostDataChangedEvent method of those controls whose LoadPostData method returned true. TheRaisePostDataChangedEvent method raises post data changed event. For example, theTextBox control raises this event when the new value of the text box is different from the old value.
13. Raise Postback Event (postback only): The page calls the RaisePostBackEvent method of the control whose associated HTML element submitted the form. For example, the Button control's associated HTML element posts the page back to the server. The RaisePostBackEvent method of a control must map the postback event to one or more server-side events. For example, the RaisePostBackEvent method of the Button control maps the postback event to the Command and Click server-side events.
14. Load Complete: The page calls its OnLoadComplete method to raise the LoadComplete eventto signal the completion of all loading activities including loading post data and raising postdata changed event to allow interested controls to update themselves accordingly.
16. PreRender: The page takes the following actions in this phase of its life cycle:
- Calls its EnsureChildControls method to ensure its child controls are created beforethe page enters its rendering phase.
- Calls its own OnPreRender method to raise its own PreRender event.
- Recursively calls the OnPreRender methods of the controls in its Controls collectionto raise their PreRender events.
18. Save Control State: The page recursively calls the SaveControlState method of those controls in its Controls collection that have called the RegisterRequiresControlState method ofthe page class to express interest in saving their control states.
19. Save View State: The page first calls its own SaveViewState method and then calls the SaveViewState method of the controls in its Controls collection to allow them to save their view states.
20. Save State Complete: The page calls its OnSaveStateComplete method to raise the SaveStateComplete event to signal the completion of all save state activities.
21. Rendering: The page takes the following actions in this phase of its life cycle:
- Creates an instance of the HtmlTextWriter class that encapsulates the output stream of the response.
- Calls its RenderControl method and passes the HtmlTextWriter instance into it.
Life-cycle Events (MSDN)
The following table lists the page life-cycle events that you will use most frequently. There are more events than those listed; however, they are not used for most page processing scenarios. If you want to write custom server controls, you need to understand more about these stages.
Use this event for the following:
- Check the IsPostBack property to determine whether this is the first time the page is being processed.
- Create or re-create dynamic controls.
- Set a master page dynamically.
- Set the Theme property dynamically.
- Read or set profile property values.
- Note: If the request is a postback, the values of the controls have not yet been restored from view state. If you set a control property at this stage, its value might be overwritten in the next event.
Raised after all controls have been initialized and any skin settings have been applied. Use this event to read or initialize control properties.
Raised by the Page object. Use this event for processing tasks that require all initialization be complete.
Use this event if you need to perform processing on your page or control before the Load event.
After the Page raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.
The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded.
Use the OnLoad event method to set properties in controls and establish database connections.
Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.
Use this event for tasks that require that all other controls on the page be loaded.
Before this event occurs:
- The Page object calls EnsureChildControls for each control and for the page.
- Each data bound control whose DataSourceID property is set calls its DataBind method. The PreRender event occurs for each control on the page. Use the event to make final changes to the contents of the page or its controls.
Before this event occurs, ViewState has been saved for the page and for all controls. Any changes to the page or controls at this point will be ignored.
Use this event perform tasks that require view state to be saved, but that do not make any changes to controls.
This is not an event; instead, at this stage of processing, the Page object calls this method on each control. All ASP.NET Web server controls have a Render method that writes out the control's markup that is sent to the browser.
If you create a custom control, you typically override this method to output the control's markup. However, if your custom control incorporates only standard ASP.NET Web server controls and no custom markup, you do not need to override the Render method. A user control (an .ascx file) automatically incorporates rendering, so you do not need to explicitly render the control in code.
This event occurs for each control and then for the page. In controls, use this event to do final cleanup for specific controls, such as closing control-specific database connections.
For the page itself, use this event to do final cleanup work, such as closing open files and database connections, or finishing up logging or other request-specific tasks.
During the unload stage, the page and its controls have been rendered, so you cannot make further changes to the response stream. If you attempt to call a method such as the Response.Write method, the page will throw an exception.
Additional Page Life Cycle Considerations
- Individual ASP.NET server controls have their own life cycle that is similar to the page life cycle. For example, a control's Init and Load events occur during the corresponding page events.
- The Init event (and also the Unload event) for each child control occur before the corresponding event is raised for its container (bottom-up). However the Load event for a container occurs before the Load events for its child controls (top-down).
- When inheriting a class from the Page class, in addition to handling events raised by the page, you can override methods from the page's base class. For example, you can override the page's InitializeCulture method to dynamically set culture information. Note that when creating an event handler using the Page_event syntax, the base implementation is implicitly called and therefore you do not need to call it in your method. For example, the base page class's OnLoad method is always called, whether you create a Page_Load method or not. However, if you override the page OnLoad method with the override keyword (Overrides in Visual Basic), you must explicitly call the base method. For example, if you override the OnLoad method on the page, you must call base.Load (MyBase.Load in Visual Basic) in order for the base implementation to be run.
ASP.NET 2.0 Page LifeCycle
Professional ASP.NET 2.0 Server Control and Component Development
ASP.NET Page Life Cycle Overview