ASP.NET Page Life Cycle

Inder2305
Posted by in ASP.NET category on for Beginner level | Views : 52909 red flag

This is my first article on asp.net and I will try to explain the basics of asp.net page life cycle and easy understanding of its usage.

Page life cycle depends on whether it is requested for the first time or it is after postback(page request of itself) and finalize to the web server. When a web page is requested to web server, it goes through a series of sequence of steps/events (like initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering) before it returned back to the web browser.


 Download source code for ASP.NET Page Life Cycle

Introduction
This is my first article on asp.net and I will try to explain the basics of asp.net page life cycle and easy understanding of its usage. 

It is very important to understand ASP.NET page life cycle for many reasons, mainly for understanding where we should place particular methods and when we should set page related properties. Additionally if we are developing custom server controls then clear understanding of page life cycle is very useful to correct initialization of control, setting properties with view-state data, and control’s code(Control events are subject to ASP.NET page only)

Page life cycle depends on whether it is requested for the first time or it is after postback(page request of itself) and finalize to the web server. When a web page is requested to web server, it goes through a series of sequence of steps/events (like initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering) before it returned back to the web browser.

When we use and manipulate page life cycle events correctly then it becomes a handy and powerful tool for developing web applications.

Background

IIS : It is default web server with Microsoft .NET. Internet Information Server helps to deploy web sites/web application. IIS web server receives request for a web resource (file), it checks the extension of the file (e.g. .aspx, ascx, .ashx and .asmx) and then it determines which ISAPI extension should handle this request and passes this request to proper ISAPI extension

ASPNET_ISAPI.DLL : IIS loads this DLL and sends the page request to this DLL. This DLL loads the HTTPRuntime for further processing.

ASPNET_WP.EXE: It contains an Application Pool. Each Application Pool can contain any number of Applications. Application Pool is also called as AppDomain. When a web page is requested, IIS looks for the application pool under which the current application is running and forwards the request to the respective worker process. 


Explanation

General Page Life Cycle Stages Each and every time the browser sends the request, page instance is created. HTTP runtime invokes ProcessRequest and starts page execution.

Table showing stage and corresponding Events 

Stage Events/Method  
Initialization of the page   Page_Init 
Loading of the View State    LoadViewState  
processing of the Post back data   LoadPostData  
Loading of Page  Page_Load  
Notification of PostBack   RaisePostDataChangedEvent 
Handling of PostBack Event   RaisePostBackEvent 
Pre Rendering of Page   Page_PreRender  
Saving of view state   SaveViewState  
Rendering of Page   Page_Render  
Unloading of the Page   Page_UnLoad  

Setting up of the page 

ASP.NET determines that the page request by a user requires to be parsing and compiling or to render cached copy of the page to be send. Thus it comes very much before of the beginning of the page life cycle. After this, it is also checked that request is a normal request, postback, cross-page postback or callback. The page constructor creates a tree of controls as soon as the HTTP runtime instantiates the page class to perform the request

Events

PreInit

This event is the beginning of the page life cycle. Every page controls are initialized and the properties are set according to aspx source code.

  • Possible to Change or set Master page, Themes
  • Creates or re-creates dynamic controls.
  • Reads or sets Profile property values.


Init

First, the Init event for the Page object occurs, then Init event occurs for each control on the Page. Viewstate information is not available at this stage.

  • Controls have been initialized zed
  • Theme skins applied if any.
  • Initialize control properties.


InitComplete


This event is used to processing tasks that require all initialization be complete.

PreLoad

This event is used before Perform any processing that should occur before Load. Use this event if you need to perform processing on your page or control before the Load event. Before the Page instance raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.

Load

Set properties in controls and establish database connections

Control Events

These are control specific events such as – Button Click, DropDownIndexChanged etc.

Load Complete

This event is used for performing those tasks which require load has been completed.

PreRender

In this event page insures that all child controls are created. Page calls EnsureChildControls for all controls, including itself. Every controls whose datasource/databind property is set calls for its databind method.

SaveStateComplete

This event occurs after viewstate encoded and saved for the page and for all controls.

Render

Every ASP.NET control has render method and the page instance calls this method to output the control’s markup, after this event any changes to the page or controls are ignored.

Unload

Unload event used to do cleanup task like closing the database connections, closing of the open files, completing logging or other requested tasks. Unload events occurs for each control on page control tree first and after that page.


Reference
For further reading on this topic, please refer to above url.
Page copy protected against web site content infringement by Copyscape

About the Author

Inder2305
Full Name: Indrajeet Sutar
Member Level: Starter
Member Status: Member
Member Since: 12/8/2009 8:13:45 AM
Country: India

http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Bijayani on: 1/25/2010
Hi,

I happened to see your post and find it quite impressive and informative. I would like to share a link where a software engineer has shared a tip on "Post Data from One form to other form using Cross-Page techiniuqe in ASP.NET 2.0".

Here is the link:
http://www.mindfiresolutions.com/post-data-from-one-form-to-other-form-using-crosspage-techiniuqe-in-aspnet-20-767.php

Hope you find it useful and of assistance.

Thanks for sharing a valuable post.
Bijayani


Login to post response

Comment using Facebook(Author doesn't get notification)