C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

ASP.NET Page Life Cycle


Posted Date: 02 May 2008    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Asha MathewsMember Level: Platinum    
Rating: 1 out of 5Points: 20



In this article, we will see the stages of execution of the ASP .NET Page.

Each request for an .aspx page that hits IIS is handed over to HTTP Pipeline. HTTP Pipeline is a chain of managed objects that sequentially process the request and convert it to plain HTML text content. The start point of HTTP Pipeline is the HttpRuntime class. The ASP.NET infrastructure creates each instance of this class per AppDomain hosted within the worker process. HttpRuntime class picks up an HttpApplication object from an internal pool and sets it to work on the request. It finds out what class has to handle the request. The association between the resources and handlers are stored in the configurable file of the application. In web.config and also in machine.config you will find these lines in section.

If you run through the following program, it will be much easier to follow

<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory"/>

This extension can be associated with HandlerClass or HandlerFactory class. HttpApplication object gets the page object that implements the IHttpHandler Interface. The process of generating the output to the browser is started when the object calls ProcessRequest method.

This extension can be associated with HandlerClass or HandlerFactory class. HttpApplication object gets the page object that implements the IHttpHandler Interface. The process of generating the output to the browser is started when the object calls ProcessRequest method.

Page Life Cycle

Once the HTTP page handler class is fully identified, the ASP.NET runtime calls the handler's ProcessRequest to start the process. This implementation begins by calling the method FrameworkInitialize(), which builds the control trees for the page. This is a protected and virtual member of TemplateControl class, class from which page itself derives.

Next the processRequest() makes page transits various phases: initialization, loading of viewstate and postback data, loading of page's user code and execution postback server-side events. Then page enters in render mode, the viewstate is updated and HTML generated is sent to the output console. Finally page is unloaded and request is considered completely served.

Stages and corresponding events in the life cycle of the ASP.NET page cycle:

Stage Events/Method

Page Initialization Page_Init
View State Loading LoadViewState
Postback data processing LoadPostData
Page Loading Page_Load
PostBack Change Notification RaisePostDataChangedEvent
PostBack Event Handling RaisePostBackEvent
Page Pre Rendering Phase Page_PreRender
View State Saving SaveViewState
Page Rendering Page_Render
Page Unloading Page_UnLoad

Some of the events listed above are not visible at the page level. It will be visible if you happen to write server controls and write a class that is derived from page.

Page Execution Stages:

The first stage in the page life cycle is initialization. This is fired after the page's control tree has been successfully created. All the controls that are statically declared in the .aspx file will be initialized with the default values. Controls can use this event to initialize some of the settings that can be used throughout the lifetime of the incoming web request. Viewstate information will not be available at this stage.

After initialization, page framework loads the view state for the page. Viewstate is a collection of name/value pairs, where control's and page itself store information that is persistent among web requests. It contains the state of the controls the last time the page was processed on the server. By overriding LoadViewState() method, component developer can understand how viewstate is restored.
Once viewstate is restored, control will be updated with the client side changes. It loads the posted data values. The PostBackData event gives control a chance to update their state that reflects the state of the HTML element on the client.

At the end of the posted data changes event, controls will be reflected with changes done on the client. At this point, load event is fired.

Key event in the life cycle is when the server-side code associated with an event triggered on the client. When the user clicks on the button, the page posts back. Page framework calls the RaisePostBackEvent. This event looks up for the event handler and run the associated delegate.

After PostBack event, page prepares for rendering. PreRender event is called. This is the place where user can do the update operations before the viewstate is stored and output is rendered. Next stage is saving view state, all the values of the controls will be saved to their own viewstate collection. The resultant viewstate is serialized, hashed, base24 encoded and associated with the _viewstate hidden field.

Next the render method is called. This method takes the HtmlWriter object and uses it to accumulate all HTML text to be generated for the control. For each control the page calls the render method and caches the HTML output. The rendering mechanism for the control can be altered by overriding this render method.

The final stage of the life cycle is unload event. This is called just before the page object is dismissed. In this event, you can release critical resources you have such as database connections, files, graphical objects etc. After this event browser receives the HTTP response packet and displays the page.






Responses

Author: Sriram    04 May 2008Member Level: Gold   Points : 2
Asp.net 2.0 Page Life cycle

Page_init
Page_load
Page_prerender
Page_AbortTransaction
Page_commitTransacton
Page_Disposed
Page_Unload




Author: venkat kamal    04 May 2008Member Level: Gold   Points : 2
Begin transaction - only if request is transacted.
init - every time a page is processed.
Load view state - only on post back.
Process post data1 - only on post back.
load - every time.
process data 2 - only on post back.
raise changed event - only on post back.
raise post back event - only on post back.
prerender - every time.
render - everytime.
end transaction - only if the request is transacted.
trace.end request - only when tracing is enabled.
unload recursive - every request.


Author: Vijaykumar Patil    05 May 2008Member Level: Gold   Points : 2
BeginTranaction - only if the request is transacted
Init - every time a page is processed
LoadViewState - Only on postback
ProcessPostData1 - Only on postback
Load - every time
ProcessData2 - Only on Postback
RaiseChangedEvent - Only on Postback
RaisePostBackEvent - Only on Postback
PreRender - everytime
BuildTraceTree - only if tracing is enabled
SaveViewState - every time
Render - Everytime
End Transaction - only if the request is transacted
Trace.EndRequest - only when tracing is enabled
UnloadRecursive - Every request



Author: kunal badgujar    05 May 2008Member Level: Gold   Points : 2
hi..

Page_init
Page_load
Page_prerender
Page_AbortTransaction
Page_commitTransacton
Page_Disposed
Page_Unload



Author: www.DotNetVJ.com    10 May 2008Member Level: Diamond   Points : 2
Hi,
Huys its important to understand that this is an article, This is not a forum to post answers but you can comment on the content of the article.
Thanks -- Vj


Author: Kamal    12 May 2008Member Level: Gold   Points : 2
For more detail check Event Life cycle of ASP.NET 2.0


Author: LOGESHWARAN    10 Feb 2009Member Level: Gold   Points : 2
Event Life cycle of ASP.NET 2.0

The events occur in the following sequence. Its best to turn on tracing() and track the flow of events :

PreInit – This event represents the entry point of the page life cycle. If you need to change the Master page or theme programmatically, then this would be the event to do so. Dynamic controls are created in this event.

Init – Each control in the control collection is initialized.

Init Complete* - Page is initialized and the process is completed.

PreLoad* - This event is called before the loading of the page is completed.

Load – This event is raised for the Page and then all child controls. The controls properties and view state can be accessed at this stage. This event indicates that the controls have been fully loaded.

LoadComplete* - This event signals indicates that the page has been loaded in the memory. It also marks the beginning of the rendering stage.

PreRender – If you need to make any final updates to the contents of the controls or the page, then use this event. It first fires for the page and then for all the controls.

PreRenderComplete* - Is called to explicitly state that the PreRender phase is completed.

SaveStateComplete* - In this event, the current state of the control is completely saved to the ViewState.

Unload – This event is typically used for closing files and database connections. At times, it is also used for logging some wrap-up tasks.

The events marked with * have been introduced in ASP.NET 2.0.


Author: sangeetha    28 Apr 2009Member Level: Gold   Points : 2
1.Page Request
2.Start
3.Page Init
4.Page Load
5.Validation
6.PostBack Event Handling
7.Page Rendering
8.Page Unload

Page Request - When the page is requested ASP.Net determines whether the page is to be parsed and compiled or a cached verion of the page is to be sent without running the page.

Start - Page propertied REQUEST and RESPONSE are SET, if the page is pastback request then the IsPostBack property is SET and in addition to this UICulture property is also SET.

Page Initilization - In this the UniqueID of each property is SET. If the request was postback the data is not yet loaded from the viewstate.

Page Load - If it was a postback request then the data gets loaded in the control from the ViewState and control property are set.

Validation - If any control validation present, they are performed and IsValid property is SET for each control.

PostBack Event Handling - If it was a postback request then any event handlers are called.

Page Rendering - Before this the viewstate is saved from the page and RENDER method of each page is called.

Page Unload - Page is fully rendered and sent to the client(Browser) and is discarded. Page property RESPONSE and REQUEST are unloaded.



Author: D.Jeya kumar(JK)    29 Apr 2009Member Level: Diamond   Points : 1
Hi,

Good post. Nice article. keep posting more Good articles

Regards
JK


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Life cycle  .  ASP.NET life cycle  .  ASP.NET events  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: What are the ways to bind data to a drop down list?
Previous Resource: Working with DataList
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use