Understanding the Page Life Cycle in ASP.NET

The 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. You can perform the update operations on the ready-to-be rendered output in the PreRender event. Use the Pre_Render event to make any last minute changes to the contents of the web page or any of its controls.
The following code snippet 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);
}
The 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. It makes use of “a text writer that writes its output to the OutputStream of the page’s Response property.” However, it should be noted that prior to rendering the web page, the view state for the web page and all its controls (provided view state for the web page and/or its controls enabled) are saved. You can trap the rendered output and make any changes on it, i.e., adding headers and footers to the rendered page, etc.
The last stage in the ASP.NET web page life cycle is the Page_UnLoad event. 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. In this method, you can perform typical cleanup activities. According to MSDN, “Unload is called after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and any cleanup is performed.” It should be noted that you cannot make any changes to the response stream in this event as the page and its controls have already been rendered.
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.
References
Please refer to the following links for related references on this topic.
http://msdn2.microsoft.com/en-us/library/ms178472.aspx
http://msdn2.microsoft.com/en-us/library/ms178473.aspx
http://www.15seconds.com/issue/020102.htm
http://www.aspfree.com/c/a/ASP.NET/ASP.NET-Life-Cycle-and-Best-Practices/
Conclusion
This article has had a detailed look at the series of steps that are involved in the page life cycle for an ASP.NET web page. I hope that the readers would find this article useful and I would welcome their comments and suggestions in this regard.]]>

Leave a comment

Your email address will not be published.