ASP.NET 2.0 Page Life Cycle – Additional Events apart from 1.1 has earlier

I have wrote this article after a quite interesting discussion last week I had with my PM Karthick and other team mates. 

Understanding the lifecycle of a page/control in ASP.NET 1+ was paramount to writing good, clean code – not to mention the steep decline in frustrating programming sessions. If you do not learn the page lifecycle, including what the events do, as well as the sequence in which they occur, you will indeed get mad and sad. Crying may occur.

In ASP.NET 1+, there were about 8 methods/events that were important to understand (about 12+ if you are a control developer). In ASP.NET 2.0, this has been increased significantly to give the developer more opportunities to inject logic at different points of the page’s life.

As I attempted to locate a good source for the new page lifecycle, I found incomplete and beta lists, which were typically out of date. I’ve compiled a list of events/methods that occur during a typical page request with as much data as I could find. Most of the method descriptions are from MSDN2.

Method

Postback

Control

Constructor

Always

All

     
Construct

Always

Page

     
TestDeviceFilter

  Page

Used to determine which device filter is in place, and use this information to decide how to display the page.

   
     
AddParsedSubObject

Always

All

Notifies the server control that an element, either XML or HTML, was parsed, and adds the element to the server control’s ControlCollection object.

   
     
DeterminePostBackMode

Always

Page

Returns a NameValueCollection object that contains the data posted back to the page. The presence of the page hidden fields VIEWSTATE and EVENTTARGET is used to help determine whether a postback event has occurred. The IsPostBack property is set when the DeterminePostBackMode method is called.

   
     
OnPreInit

Always

Page

Called at the beginning of the page initialization stage. After the OnPreInit method is called, personalization information is loaded and the page theme, if any, is initialized. This is also the preferred stage to dynamically define a PageTheme or MasterPage for the Page.

   
     
OnInit

Always

All

Performs the initialization and setup steps required to create a Page instance. In this stage of the page’s life cycle, declared server controls on the page are initialized to their default state; however, the view state of each control is not yet populated. A control on the page cannot access other server controls on the page during the Page_Init phase, regardless of whether the other controls are child or parent controls. Other server controls are not guaranteed to be created and ready for access.

   
     
OnInitComplete

Always

Page

Called after page initialization is complete. In this stage of the page’s life cycle, all declared controls on the page are initialized, but the page’s view state is not yet populated. You can access server controls, but they will not yet contain information returned from the user.

   
     
LoadPageStateFromPersistenceMedium

Postback

Page

Uses the Load method of the System.Web.UI.PageStatePersister object referenced by the PageStatePersister property to load any saved view-state information for the Page object.

   
     
LoadControlState

Postback

All

Restores control-state information from a previous page request that was saved by the SaveControlState method.

   
     
LoadViewState

Postback

All

Restores view-state information from a previous page request that was saved by the SaveViewState method.

   
     
OnPreLoad

Always

Page

Called after all postback data returned from the user is loaded. At this stage in the page’s life cycle, view-state information and postback data for declared controls and controls created during the initialization stage are loaded into the page’s controls. Controls created in the OnPreLoad method will also be loaded with view-state and postback data.

   
     
OnLoad

Always

All

Notifies the server control that it should perform actions common to each HTTP request for the page it is associated with, such as setting up a database query. At this stage in the page lifecycle, server controls in the hierarchy are created and initialized, view state is restored, and form controls reflect client-side data.

   
     
RaisePostBackEvent

Postback

All

Notifies the server control that caused the postback that it should handle an incoming postback event.

   
     
OnLoadComplete

Always

Page

At this point in the page life cycle, all postback data and view-state data is loaded into controls on the page.

   
     
OnPreRender

Always

All

Notifies the server control to perform any necessary prerendering steps prior to saving view state and rendering content.

   
     
OnPreRenderComplete

Always

Page

At this stage of the page life cycle, all controls are created and the page is ready to render the output. This is the last event called before the page’s view state is saved.

   
     
SaveControlState

Always

All

Saves any server control state changes that have occurred since the time the page was posted back to the server. If there is no state associated with the control, this method returns a null reference. Custom controls using control state must call the RegisterRequiresControlState method on the Page before saving control state.

   
     
SaveViewState

Always

All

Saves any server control view-state changes that have occurred since the time the page was posted back to the server. If there is no view state associated with the control, this method returns a null reference.

   
     
SavePageStateToPersistenceMedium

Always

Page

Saves any view-state and control-state information for the page. The SavePageStateToPersistenceMedium method uses the Save method of the System.Web.UI.PageStatePersister object referenced by the PageStatePersister property to store view-state and control-state information for the page.

   
     
Render

Always

All

Initializes the HtmlTextWriter object and calls on the child controls of the Page to render. The Render method is responsible for creating the text and markup that is sent to the client browser. The default Render method calls RenderChildren to write the text and markup for the controls contained on the page.

   
     
OnUnload

Always

All

Used to do target-specific processing in the Unload stage of the control lifecycle. Typically, these are cleanup functions that precede disposition of the control.

   
     

If I’m missing any significant methods, please comment.

Advertisements

2 thoughts on “ASP.NET 2.0 Page Life Cycle – Additional Events apart from 1.1 has earlier

  1. Pingback: FlasheR!

  2. Pingback: Top 10 links for understanding asp.net page life cycle | Webpru

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s