Typical Asp.net page life cycle : Standalone page , with User Control Embedded , As Content Page within Master Page , As when User Control & Content Page both on Master Page
What can be best source for understanding the asp.net page life cycle other than MSDN itself…? I am just going to refer the same and mentioned key points of for simple to understand and remember. Let’s start with Standalone aspx page… Then rest scenarios will be just change in the sequence of the events while getting called.
Typical Asp.net page life cycle: Standalone page
Page Life Cycle Events with calling sequence
· PreInit
· Init
· InitComplete
· PreLoad
· Load
· Control events
· LoadComplete
· PreRender
· PreRenderComplete
· SaveStateComplete
· Render
· Unload
Along with the events mentioned above, there some methods which are being called before and after the Events to facilitate functionality. Most of the times developers not aware ( don’t care !! ) about these methods other than other than specific cases like setting something before or after the method call or overloading them, so not covered in detailed.
Also please note that some of these methods are called recursively multiple times depending on the content of the page (e.g. postback or not etc)
Event |
When is getting called |
Page Methods getting called Before or during this event call |
|
Typical use /Functionality handled by |
|
PreInit |
Raised after the start stage is complete and |
Construct() ProcessRequest() InitializeCulture() DeterminePostBackMode() OnPreInit()
|
|
Check IsPostBack property to determine (If the request is a
Typically event is useful for – Create or re-create dynamic controls ; Set a master page dynamically. Set the Theme property dynamically. Read or set profile property values.
|
|
Init |
Raised after all controls have been initialized and any skin
|
OnInit() |
|
To read or initialize control properties.
The Init event of individual controls
|
|
InitComplete |
Raised at the end of the page’s initialization stage. |
TrackViewState() OnInitComplete() |
|
Only one operation takes place between the Init and InitComplete
View state tracking enables controls to persist
Until view
Controls typically turn on view state tracking
Use this event to make changes to view state that |
|
PreLoad |
Raised after the page loads view state for itself and all
|
LoadPageStateFromPersisteceMedium ( Postback Only)
LoadControlState(Postback only)
LoadViewState ( Postback Only)
ProcessPostData ( Postback Only) IsPostBackDataHandler.
LoadPostData (Postback
OnPreLoad() |
|
The OnPreLoad method is called after all
At this stage in the page’s life cycle, view-state
Controls created in the OnPreLoad method |
|
Load |
The Page object calls the OnLoad method on the Page object, and then recursively does the same for each
The Load event of individual controls occurs after the Load event of |
OnLoad() |
|
Use the OnLoad event method to set |
|
Control events TextChanged() or Click() etc |
|
RaisePostBackEvent() Validate() |
|
Use these events to handle specific control
In a postback request, if the page contains Check the IsValid property of the Page and |
|
LoadComplete |
Raised at the end of the event-handling stage. |
OnLoadComplete()
|
|
Use this event for tasks that require that all |
|
PreRender |
Raised after the Page object has created
The PreRender event of individual |
EnsureChildControls() OnPreRender() |
|
Use the event to make final changes to the |
|
PreRenderComplete |
Raised after each data bound control whose DataSourceID property |
Data Binding Events OnPreRenderComplete() |
|
|
|
SaveStateComplete |
Raised after view state and control state have been saved for |
SaveViewState()
SavePageStateToPersistenceMedium
OnSaveStateComplete |
|
Any changes to the page or controls at this point affect |
|
Render |
It is the processing Stage, not an event, Page |
RenderControl() Render() RenderChildren()
|
|
If you create a custom control, you typically override this
However, if your custom control incorporates only standard A user control (an .ascx file) automatically incorporates |
|
Unload |
Raised for each control and then for the page. |
OnUnload() Dispose() |
|
For Controls – use this event to do final cleanup for specific
For Page – use this event to do final cleanup work, such as
During the unload stage, the page and its controls have been
|
For those who don’t know View State Tracking –
Control.TrackViewState Method It Causes tracking of view-state changes to the server control so they can be stored in the server control's StateBag object. This object is accessible through the Control.ViewState property.Use this method when you have to develop template data-bound controls.
This method alerts ASP.NET to monitor changes to a server control's view state, which is required when you override the Control.DataBind method. The control then uses the IsTrackingViewState property to determine whether view-state change tracking is enabled for the control. If it is not enabled, the TrackViewState method is called.
Ref – MSDN
Some important Event Methods in above Page Life cycle :
LoadControlState(postback only): The page recursively calls the LoadControlState method of those controls in its Controls collections that have called the RegisterRequiresControlState method of the page class to express interest in using their control states.
LoadViewState(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.
LoadPostData(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 TextBox control assigns the new value of the text box to the Text property of the TextBox control.
LoadPostData(postback only second try): The page calls the LoadPostData method of those controls that where programmatically added to its Controls collection in the Load phase if they implement the IPostBackDataHandler interface.
RaisePostDataChangedEvent(Postback only): The page calls the RaisePostDataChangedEvent method of those controls whose LoadPostData method returned true. The RaisePostDataChangedEvent method raises post data changed event. For example, TextBox control raises this event when the new value of the text box is different from the old value.
RaisePostbackEvent (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.
Overall Page life cycle with the Events & methods
- PreInit
- Init
- InitComplete
- LoadControlState (Postback only)
- LoadViewState (Postback only)
- LoadPostData (Postback only – first try)
- PreLoad
- Load
- LoadPostData (Postback only – second try)
- RaisePostDataChangedEvent (Postback only)
- RaisePostbackEvent (Postback only)
- LoadComplete
- RaiseCallbackEvent (Postback and Callback only)
- PreRender
- PreRenderComplete
- SaveControlState
- SameViewState
- SaveStateComplete
- Render
Now let’s say we have User Control on the asp.net webpage then Asp.net Page life cycle will be as follows
- PreInit – Page
- Init – UserControl
- Init – Page
- InitComplete – Page
- LoadPageStateFromPersistenceMedium – Page
- ProcessPostData (first try) – Page
- PreLoad – Page
- Load – Page
- Load – UserControl
- ProcessPostData (second try) – Page
- RaiseChangedEvents – Page
- RaisePostBackEvent – Page
- Control Events – Page
- Control Event – UserControl
- DataBinding – Page
- DataBinding – UserControl
- LoadComplete – Page
- PreRender – Page
- PreRender – UserControl
- PreRenderComplete – Page
- SaveViewState – Page
- SavePageStateToPersistenceMedium – Page
- SaveStateComplete – Page
- Unload – UserControl
- Unload – Page
Please refer to previous post for the Asp.net Life Cycle details
There can be the case that you have one Master page in which you asp.net web page is content page , in that case Asp.net LifeCycle will be as follows
- PreInit – Page
- Init – MasterPage
- Init – Page
- InitComplete – Page
- LoadPageStateFromPersistenceMedium – Page
- LoadControlState (Postback only) – Page
- LoadViewState (Postback only) – Page
- ProcessPostData (first try) – Page
- LoadPostData (Postback only – first try) – Page
- PreLoad – Page
- Load – Page
- Load – MasterPage
- ProcessPostData (second try) – Page
- RaiseChangedEvents – Page
- RaisePostBackEvent – Page
- Control Events – OnClick,TextChanged,GridView Events followed by DataBinding
- DataBinding – Page
- DataBinding – MasterPage
- DataBinding – UserControl
- DataBinding – ChildUserControl
- LoadComplete – Page
- PreRender – Page
- PreRender – MasterPage
- PreRenderComplete – Page
- SaveViewState – Page
- SavePageStateToPersistenceMedium – Page
- SaveStateComplete – Page
- Unload – MasterPage
- Unload – Page
There is one scenario given by mbanavige on Asp.net Forums(http://forums.asp.net/t/1191194.aspx) I am just going to refer the same ,
Our test setup is composed of a Page, MasterPage, UserControl, Nested UserControl and Button control as follows: The Page is tied to the MasterPage,The UserControl is on the Page,The Nested UserControl is on the UserControl,The Button is on the Nested UserControl.,Clicking the Button calls the Page.DataBind method
Then in above mentioned case asp.net page life cycle will be as follows
- PreInit – Page
- Init – Nested ChildUserControl
- Init – UserControl
- Init – MasterPage
- Init – Page
- InitComplete – Page
- LoadPageStateFromPersistenceMedium – Page
- ProcessPostData (first try) – Page
- PreLoad – Page
- Load – Page
- Load – MasterPage
- Load – UserControl
- Load – Nested ChildUserControl
- ProcessPostData (second try) – Page
- RaiseChangedEvents – Page
- RaisePostBackEvent – Page
- Click – Button – Nested ChildUserControl
- DataBinding – Page
- DataBinding – MasterPage
- DataBinding – UserControl
- DataBinding – Nested ChildUserControl
- LoadComplete – Page
- PreRender – Page
- PreRender – MasterPage
- PreRender – UserControl
- PreRender – Nested ChildUserControl
- PreRenderComplete – Page
- SaveViewState – Page
- SavePageStateToPersistenceMedium – Page
- SaveStateComplete – Page
- Unload – Nested ChildUserControl
- Unload – UserControl
- Unload – MasterPage
- Unload – Page
Interview quickies:
1) If we have a page called "Default" and a Master page called "DefaultMaster".
Both contain Page Load events. When a page request comes to Default page, which Page Load event will call first
Ans : Default.aspx Page load
2) If we have a page called "Default" and a Master page called "DefaultMaster".
For both Page.Init events is handled. When a page request comes to Default page, which Page Init () event will call first
Ans:- DefaultMaster.Master page init event followed by Default.aspx
In Case of Nested Master Pages –
Main Master Page Init ===> Nested/Sub Master page Init ===>
Content Page Init /Requester Page.aspx
If you are still unsatisfied with asp.net life cycle events
Reference :
http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx