When a web page is sent to the Web Server for processing, it goes through a sequence of steps before it finally gets displayed in the Web Browser.
From Web Browser to IIS
When a POST request is initiated from the client side, the Web Server traps the request and it is usually routed to an .aspx web page. The request is actually routed to the HTTP Pipeline, a chain of managed objects.
After the HTTP Page handler class is identified, the ProcessRequest() method is called which eventually fires the different page events in the life cycle of a web page.
- Page_Init
- LoadViewState
- LoadPostData
- Page_Load
- RaisePostDataChangedEvent
- RaisePostBackEvent
- Page_PreRender
- SaveViewState
- Page_Render
- Page_UnLoad
All these events are associated with their respective handlers and you can even override them to customize their default behavior. The following section discusses each of these events in detail.
The Page Life Cycle Events Explained
Page_Init is first event to be triggered in the page life cycle. It is responsible for the initialization activities that are essential to create a page instance. In this phase of the page life cycle, all the server controls of the web page are initialized to their default values. However, it should be noted that the View State for a page is not available at this stage of the page life cycle and a server control of the page cannot access other server controls of the page at this phase.
You can use the Page_Init event to create or re-create the controls that need to be created or re-created dynamically. The following example illustrates how you can override the OnInit() method.
protected override void OnInit(EventArgs e)
{
if (Page != null)
{
Page.Trace.Write (“The OnInit method has been called”);
base.OnInit(e);
Page.RegisterRequiresPostBack(this);
}
}
LoadViewState : “The load view state stage only happens when the page has been posted back. During this stage the view state data saved from the previous page visit is loaded and recursively populated into the Page’s control hierarchy”. This method restores the View State information of a web page that was last saved using the SaveViewState method. You can override the LoadViewState() method to get an idea on how the viewstate is actually restored.
The following code example illustrates how you can override the LoadViewState() method.
protected override void LoadViewState(Object viewState)
{
Page.Trace.Write (“The LoadViewState method has been called.”);
if (viewState == null)
{
base.LoadViewState(viewState);
}
}
The LoadPostBackData event is fired next that processes the data that was last posted to the server for all the server controls that requires the same. Note that all controls in an ASP.NET web page is identified by a unique id. The runtime now parses the controls to match their unique ids against the Name Value Collection in the View State for the control (that has its View State enabled) and updates the control with the posted data.
The next event that gets fired is the Page_Load event that typically restores the page’s control values.
protected override void OnLoad(EventArgs e)
{
if(!IsPostBack)
{
//Code to bind static data to the controls
}
}
The RaisePostBackData and the RaisePostBackEvent events call their respective handlers if the request was a postback. The RaisePostBackEvent that executes next “notifies the server control that caused the postback that it should handle an incoming postback event.”
Pre_Render event is triggered next and you can perform any update operations that you require prior to saving the View State and rendering of the web page content.
The following code illustrates how you can override the OnPreRender() method.
protected override void OnPreRender (EventArgs e)
{ Page.Trace.Write (“The OnPreRender has been called.”);
//Any custom code
base.OnPreRender(e);
}
SaveViewState that is called next saves the View State for the web page and its controls in the hidden field called __viewstate that is associated with every web page. Note that the SaveViewMethod that is associated with the SaveViewState event returns a null reference if there is no view state associated with the control. Refer to the code snippet below that illustrates how you can override the SaveViewState() method.
protected override object SaveViewState ()
{ Page.Trace.Write (“The SaveViewState method has been called.”);
//Write your custom code here.
return base.SaveViewState();
}
The rendering event is the next stage in the web page life cycle. This actually is not an event. Rather, it is a state of processing where the Page instance executes this method on all the controls of the web page to eventually render the web page content to the web browser at the client or the requestor side.
Page_UnLoad This event is called prior to the disposal of the web page and usually contains the code that is responsible for releasing of resources that were acquired in the earlier stages of the page life cycle. Once this is done, the web browser receives the “HTTP response packet” and the web page is displayed in the web browser
The web browser now receives the HTTP response packet and displays the same in the web page. This concludes the page life cycle of an ASP.NET web page.
Nice post to remind some basics