Page Life Cycle in ASP.Net


Page Life Cycle in ASP.Net (Short description):-

The page life cycle phases are:

  • Initialization
  • Instantiation of the controls on the page
  • Restoration and maintenance of the state
  • Execution of the event handler codes
  • Page rendering

In general page in page life cycle, page goes through the below stages:-
1.     Start
·         In the start stage, page properties such as Request and Response are set.
·         At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property.
·         The page also sets the UICulture property.

2.     Initialize
·         During page initialization, controls on the page are available and each control's UniqueID property is set.
·         A master page and themes are also applied to the page if applicable.
·         If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.
3.     Load
·         During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.
4.     Validate
·         Sets the IsValid property of individual validator controls and of the page.

5.     Event Handling
·         If the request is a postback, control event handlers are called. After that, the Validate method of all validator controls is called

(NOTE: - There is an exception to this sequence: the handler for the event that caused validation is called after validation.)
6.     Render
·         Before rendering, view state is saved for the page and all controls.
·          During the rendering stage, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream object of the page's Response property.
7.     Unload
·         The Unload event is raised after the page has been fully rendered, and page is ready to send browser.
·         At this point, page properties such as Response and Request are unloaded and cleanup is performed.

This can be easily remember as S I L V E R - U

There are application stages that occur before and after a request but are not specific to a page, as explained earlier.

Within each stage of the life cycle of a page, the page raises events.  You can find detail description of this methods at MSDN site. For short description you can refer below table.

Page Event
Details
PreInit
Raised after the start stage is complete and before the initialization stage begins.

Uses:
  • Check the IsPostBack property to determine whether this is the first time the page is being processed, IsCallback and IsCrossPagePostBack properties have also been set
  • Create or re-create dynamic controls.
  • Set a master page and Theme dynamically.
Init
Raised after all controls have been initialized, and set initial value and any skin settings have been applied.

NOTE: - The Init event of individual controls occurs before the Init event of the page.
Use this event to read or initialize control properties.
InitComplete
Raised at the end of the page's initialization stage.
This is the first place where ViewState has been loaded and can be changed.
PreLoad
Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance.
Load
The Page object calls the OnLoad method on the Page object
Use the OnLoad event method to set properties in controls and to establish database connections.

NOTE: - The Load event of individual controls occurs after the Load event of the page.
Control events
Use these events to handle specific control events,
Such as a Button control's Click event or a TextBox control's TextChanged event.
LoadComplete
Use this event for tasks that require that all other controls on the page be loaded.
PreRender
Raised after the Page object has created all.
The Page object raises the PreRender event on the Page object, and then recursively does the same for each child control.
This is the last page where the visual properties of the controls can be changed before getting them displayed on the page.
PreRenderComplete
This will be called when the page is ready and no changes in visual elements can be made. All data binding are done at this point.
SaveStateComplete
Raised after view state and control state have been saved for the page and for all controls.
Any changes to the page or controls at this point affect rendering, but the changes will not be retrieved on the next postback.
Render
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 aRender method that writes out the control's markup to send to the browser.
Unload
Raised 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.


Comments