Академический Документы
Профессиональный Документы
Культура Документы
Not quite what you are looking for? You may want to try: Understanding ASP.NET Application and Page Life Cycle - A Beginner's Tutorial ASP.NET Page Life Cycle
9,958,891 members (46,160 online) 1 Dheer Rajpoot
highlights off
290 Sign out
home
articles
quick answers
discussions
features
community
help
Next
Article Browse Code Stats Revisions (7) Alternatives Comments & Discussions (145)
About Article
ASP.NET application and page life cycle Type Licence Article CPOL 17 Apr 2010 471,936 6,308 562 times
Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions.
ASP.NET Dev
Table of Contents
Introduction The Two Step Process Creation of ASP.NET Environment Process Request using MHPM Events Fired In What Event We Should Do What? A Sample Code for Demonstration Zooming ASP.NET Page Events About the Source Code References Can you explain ASP.NET application and page life cycle(Demo, in which event we should do what) Part 3?
Top News
Internet Explorer 11: Dont call me IE
Get the Insider News free each morning.
Related Videos
Introduction
In this article, we will try to understand what the different events are which take place right from the time the user sends a request, until the time the request is rendered on the browser. So we will first try to understand the two broader steps of an ASP.NET request and then we will move into different events emitted from H t t p H a n d l e r , H t t p M o d u l e and ASP.NET page object. As we move in this event journey, we will try to understand what kind of logic should go in each and every one of these events. This is a small Ebook for all my .NET friends which covers topics like WCF, WPF, WWF, Ajax, Core .NET, SQL, etc. You can download the same from here or else you can catch me on my daily free training here.
Related Articles
Understanding ASP.NET Application and Page Life Cycle - A Beginner's Tutorial
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
1/14
7/8/13
ASP.NET Page Life Cycle The ASP.NET Page Lifecycle A Basic Approach Callback WebControls Introducing ASP.NET Page Modules A comparison between ASP.NET 1.x and ASP.NET 2.0 ViewState and Server.Transfer Best practices ASP.NET Web Form Model with Partial Rendering and Events Beginner's Walk - Web Development Extending ASP.NET role based Security with Custom Security Module (Permission Based, Page Level Authorization) Back to the basics: Exploration of approaches to handle ThreadAbortException with Response.Redirect() A Walkthrough to Application State Understanding ASP.NET MVC (Model View Controller) Architecture for Beginners Why(s) & How(s) of Asp.Net MVC Part 1 Six common uses of the Template Design Pattern: Design Pattern series An Absolute Beginner's Tutorial on ASP.NET MVC for Web Forms Developers Master the .NET Code Model ViewState: Various ways to reduce performance overhead How to skip calling Page_Load event ASP.NET Page Life Cycle Events
In the coming sections, we will understand both these main steps in more detail.
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
2/14
7/8/13
The below image explains how the internal object model looks like for an ASP.NET request. At the top level is the ASP.NET runtime which creates an A p p d o m a i n which in turn has H t t p R u n t i m e with request, response and context objects.
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
3/14
7/8/13
If you want to inject logic in the events of ASP.NET pipleline, then you use H t t p M o d u l e . ASP.NET. In other words, H t t p M o d u l e is an event based processor.
You can read more about the differences from here. Below is the logical flow of how the request is processed. There are 4 important steps MHPM as explained below: Step 1(M: HttpModule): Client request processing starts. Before the ASP.NET engine goes and creates the ASP.NET H t t p M o d u l eemits events which can be used to inject customized logic. There are 6 important events which you can utilize before your page object is created B e g i n R e q u e s t , A u t h e n t i c a t e R e q u e s t ,A u t h o r i z e R e q u e s t ,R e s o l v e R e q u e s t C a c h e ,A c q u i r e R e q u e s t S t a t e and P r e R e q u e s t H a n d l e r E x e c u t e . Step 2 (H: HttpHandler): Once the above 6 events are fired, ASP.NET engine will invoke P r o c e s s R e q u e s tevent if you have implemented H t t p H a n d l e rin your project. Step 3 (P: ASP.NET page): Once the H t t p H a n d l e r logic executes, the ASP.NET page object is created. While the ASP.NET page object is created, many events are fired which can help us to write our custom logic inside those page events. There are 6 important events which provides us placeholder to write logic inside ASP.NET page s I n i t ,L o a d ,v a l i d a t e ,e v e n t ,r e n d e rand u n l o a d . You can remember the word S I L V E Rto remember the events S Start (does not signify anything as such just forms the word) , I (Init) , L (Load) , V (Validate), E (Event) and R (Render). Step4 (M: HttpModule): Once the page object is executed and unloaded from memory, H t t p M o d u l e provides post page execution events which can be used to inject custom post-processing logic. There are 4 important post-processing events P o s t R e q u e s t H a n d l e r E x e c u t e ,R e l e a s e r e q u e s t S t a t e , U p d a t e R e q u e s t C a c h eand E n d R e q u e s t . The below figure shows the same in a pictorial format.
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
4/14
7/8/13
H t t p M o d u l e B e g i n R e q u e s t
H t t p M o d u l e A u t h e n t i c a t e R e q u e s t
H t t p M o d u l e A u t h o r i z e R e q u e s t
H t t p M o d u l e R e s o l v e R e q u e s t C a c h e
H t t p M o d u l e A c q u i r e R e q u e s t S t a t e
This event is raised just prior to handling control to the H t t p H a n d l e r . Before you want the control to H t t p M o d u l e P r e R e q u e s t H a n d l e r E x e c u t e be handed over to the handler any pre-processing you would like to do.
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
5/14
7/8/13
P a g e
I n i t
Any setting initialization. Master page s and the settings. In this section, we do not have access to viewstate, postedvalues and neither the controls are initialized. In this section, the ASP.NET controls are fully loaded and you write UI manipulation logic or any other logic over here. If you have valuators on your page , you would like to check the same here. Its now time to send the output to the browser. If you would like to make some changes to the final HTML which is going out to the browser, you can enter your HTML logic here. Page object is unloaded from the memory. Any logic you would like to inject after the handlers are executed. If you would like to save update some state variables like session variables. Before you end, if you want to update your cache. This is the last stage before your output is sent to the client browser.
P a g e
L o a d
P a g e
V a l i d a t e
R e n d e r
P a g e
U n l o a d
H t t p M o d u l e P o s t R e q u e s t H a n d l e r E x e c u t e H t t p M o d u l e R e l e a s e r e q u e s t S t a t e H t t p M o d u l e U p d a t e R e q u e s t C a c h e H t t p M o d u l e E n d R e q u e s t
p u b l i cc l a s sc l s H t t p M o d u l e:I H t t p M o d u l e { . . . . . . v o i dO n U p d a t e R e q u e s t C a c h e ( o b j e c ts e n d e r ,E v e n t A r g sa ) { o b j A r r a y L i s t . A d d ( " h t t p M o d u l e : O n U p d a t e R e q u e s t C a c h e " ) ; } v o i dO n R e l e a s e R e q u e s t S t a t e ( o b j e c ts e n d e r ,E v e n t A r g sa ) { o b j A r r a y L i s t . A d d ( " h t t p M o d u l e : O n R e l e a s e R e q u e s t S t a t e " ) ; } v o i dO n P o s t R e q u e s t H a n d l e r E x e c u t e ( o b j e c ts e n d e r ,E v e n t A r g sa ) { o b j A r r a y L i s t . A d d ( " h t t p M o d u l e : O n P o s t R e q u e s t H a n d l e r E x e c u t e " ) ; } v o i dO n P r e R e q u e s t H a n d l e r E x e c u t e ( o b j e c ts e n d e r ,E v e n t A r g sa ) { o b j A r r a y L i s t . A d d ( " h t t p M o d u l e : O n P r e R e q u e s t H a n d l e r E x e c u t e " ) ; } v o i dO n A c q u i r e R e q u e s t S t a t e ( o b j e c ts e n d e r ,E v e n t A r g sa ) { o b j A r r a y L i s t . A d d ( " h t t p M o d u l e : O n A c q u i r e R e q u e s t S t a t e " ) ; } v o i dO n R e s o l v e R e q u e s t C a c h e ( o b j e c ts e n d e r ,E v e n t A r g sa ) { o b j A r r a y L i s t . A d d ( " h t t p M o d u l e : O n R e s o l v e R e q u e s t C a c h e " ) ; } v o i dO n A u t h o r i z a t i o n ( o b j e c ts e n d e r ,E v e n t A r g sa ) { o b j A r r a y L i s t . A d d ( " h t t p M o d u l e : O n A u t h o r i z a t i o n " ) ; } v o i dO n A u t h e n t i c a t i o n ( o b j e c ts e n d e r ,E v e n t A r g sa ) { o b j A r r a y L i s t . A d d ( " h t t p M o d u l e : A u t h e n t i c a t e R e q u e s t " ) ; } v o i dO n B e g i n r e q u e s t ( o b j e c ts e n d e r ,E v e n t A r g sa ) { o b j A r r a y L i s t . A d d ( " h t t p M o d u l e : B e g i n R e q u e s t " ) ;
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
6/14
7/8/13
We are also tracking all the events from the ASP.NET page .
Collapse | Copy Code
p u b l i cp a r t i a lc l a s s_ D e f a u l t:S y s t e m . W e b . U I . P a g e { p r o t e c t e dv o i dP a g e _ i n i t ( o b j e c ts e n d e r ,E v e n t A r g se ) { c l s H t t p M o d u l e . o b j A r r a y L i s t . A d d ( " P a g e : I n i t " ) ; } p r o t e c t e dv o i dP a g e _ L o a d ( o b j e c ts e n d e r ,E v e n t A r g se ) { c l s H t t p M o d u l e . o b j A r r a y L i s t . A d d ( " P a g e : L o a d " ) ; } p u b l i co v e r r i d ev o i dV a l i d a t e ( ) { c l s H t t p M o d u l e . o b j A r r a y L i s t . A d d ( " P a g e : V a l i d a t e " ) ; } p r o t e c t e dv o i dB u t t o n 1 _ C l i c k ( o b j e c ts e n d e r ,E v e n t A r g se ) { c l s H t t p M o d u l e . o b j A r r a y L i s t . A d d ( " P a g e : E v e n t " ) ; } p r o t e c t e do v e r r i d ev o i dR e n d e r ( H t m l T e x t W r i t e ro u t p u t ) { c l s H t t p M o d u l e . o b j A r r a y L i s t . A d d ( " P a g e : R e n d e r " ) ; b a s e . R e n d e r ( o u t p u t ) ; } p r o t e c t e dv o i dP a g e _ U n l o a d ( o b j e c ts e n d e r ,E v e n t A r g se ) { c l s H t t p M o d u l e . o b j A r r a y L i s t . A d d ( " P a g e : U n L o a d " ) ; } }
Below is how the display looks like with all events as per the sequence discussed in the previous section.
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
7/14
7/8/13
Now between these HTML controls coming live on the server as ASP.NET controls, the ASP.NET page emits out lot of events which can be consumed to inject logic. Depending on what task / logic you want to perform, we need to put this logic appropriately in those events. Note : Most of the developers directly use the p a g e _ l o a d method for everything, which is not a good thought. So its either populating the controls, setting view state, applying themes, etc., everything happens on the page load. So if we can put logic in proper events as per the nature of the logic, that would really make your code clean.
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
8/14
7/8/13
Seq Events Controls Initialized
Init
No
No
No
You can access view state and any synch logic where you want viewstate to be Not pushed to behind code variables can be guaranteed done here. You can access form data. Any logic where you want the form data to be pushed to behind code variables can be done here. This is the place where you will put any logic you want to operate on the controls. Like flourishing a combobox from the database, sorting data on a grid, etc. In this event, we get access to all controls, viewstate and their posted values. If your page has validators or you want to execute validation for your page , this is the right place to the same. If this is a post back by a button click or a dropdown change, then the relative events will be fired. Any kind of logic which is related to that event can be executed here. If you want to make final changes to the UI objects like changing tree structure or property values, before these controls are saved in to view state. Once all changes to server controls are done, this event can be an opportunity to save control data in to view state. If you want to add some custom HTML to the output this is the place you can. Any kind of clean up you would like to do here.
PostBackdata
Yes
Load
Yes
Yes
Yes
Validate
Yes
Yes
Yes
Event
Yes
Yes
Yes
Pre-render
Yes
Yes
Yes
Yes
Yes
Yes
9 10
Yes Yes
Yes Yes
Yes Yes
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
9/14
7/8/13
References
I am not so smart to write this article by myself ;-), lot of things I have plugged from the below articles. Read more about IIS 7.0 life cycle http://msdn.microsoft.com/en-us/library/bb470252.aspx Intercepting filters http://msdn.microsoft.com/en-us/library/ms998536.aspx Explains how to implement Httphandlers and modules http://msdn.microsoft.com/enus/library/system.web.httpapplication.aspx Httphandlers and Httpmodules :- http://www.15seconds.com/Issue/020417.htm Implementing security using modules and handlers http://joel.net/articles/asp.net2_security.aspx Difference between Httpapplication and global.asax http://codebetter.com/blogs/karlseguin/archive/2006/06/12/146356.aspx
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
10/14
7/8/13
Shivprasad koirala
Architect http://www.questpond.com India
I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. Do visit my site for .NET, C# , design pattern , WCF , Silverlight , LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training and Interview questions and answers
Article Top
Like
100
14
Tw eet
24
Rate this:
Poor
Excellent
Vote
My vote of 5
Excellent
Reply Email View Thread Permalink Bookmark
SagarRS
28-Jun-13 7:47
What tools or software do you use to edit the pictures in your article
Dream8Lee
18-Jun-13 4:46
May I ask you what tools or software do you use to edit the pictures in your article? If you can tell me I would be very grateful.
Reply Email View Thread Permalink Bookmark
Re: What tools or software do you use to edit the pictures in your article
http://www.balsamiq.com/[^]
Shivprasad koirala
18-Jun-13 4:48
My book .NET interview questions with 500 mostly asked questions in .NET world .NET Interview questions and answers
5.00/5 (1 vote)
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
11/14
7/8/13
Re: What tools or software do you use to edit the pictures in your article
Dream8Lee
18-Jun-13 5:03
Thank you very much. I had spent a lot of time but fail to find its name because I don't know how to describe my question. I very like its style.
Reply Email View Thread Permalink Bookmark
Sandeep Akhare
12-Jun-13 12:05
If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog
My vote of 5
great article
Reply Email View Thread Permalink Bookmark
Naufel Basheer
6-Jun-13 2:59
Nice Job
Erik Ochoa
23-May-13 11:14
Thanks for this article it's very helpful, you open my eyes ASP.NET lifecycle.
My vote of 5
excellent article,,so simple and clear ,, Thanks
Reply Email View Thread Permalink Bookmark
Mithileshchandan
16-May-13 1:52
My vote of 5
umeryounas
25-Apr-13 2:23
My vote of 5
Very nice graphic illustrations and good content. 5.
Reply Email View Thread Permalink Bookmark
GregoryW
22-Apr-13 5:01
Shripati Nirmalaa
18-Apr-13 3:35
Hi Shiv, I found this article very useful for intermediate level developers. Always been expecting new articles. Best of luck and keep rocking.
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
12/14
7/8/13
My vote of 5
such great great article
Reply Email View Thread Permalink Bookmark
harshavardhanreddy
5-Apr-13 5:42
My vote of 5
Nice article. Easy to understand.. thank you ....
Reply Email View Thread Permalink Bookmark
mahesh27
25-Mar-13 23:11
My vote of 5
Nice article. very clarifying.
Reply Email View Thread Permalink Bookmark
Vick_Web
22-Mar-13 5:42
My vote of 5
great
Reply Email View Thread Permalink Bookmark
5-Mar-13 12:53
My vote of 4
Good Work
Reply Email View Thread Permalink Bookmark
Naseer A Khan
2-Mar-13 5:51
My vote of 4
Nice article and some nice diagrams!
Reply Email View Thread Permalink Bookmark
Reonekot
19-Feb-13 14:52
Video Part 2
That was excellent, both video and article. I'm really interested to watch your other video. Part 2
Spring77
14-Feb-13 16:44
cause the one that you've shared in this page is part3.. am wondering where the part2 is?
Reply Email View Thread Permalink Bookmark 5.00/5 (1 vote) wbbman 16-Apr-13 15:04
I'd also like to see Part 1 and Part 2 of the video. I also search Vimeo, where the videos are stored, but was not able to find it. Great article and video. Does anyone know how to find Part 1 and Part 2 videos?
Reply Email View Thread Permalink Bookmark
My vote of 5
SleepyCrat
13-Feb-13 11:05
This is an excellent article for any level of developer. Good job man.
Reply Email View Thread Permalink Bookmark 1.00/5 (1 vote) jfos 13-Feb-13 6:36
13/14
7/8/13
My vote of 5
excellent
Reply Email View Thread Permalink Bookmark
nallapula
12-Feb-13 1:29
My vote of 3
Good one
Reply Email View Thread Permalink Bookmark
My vote of 5
great...
Reply Email View Thread Permalink Bookmark
11-Feb-13 1:31
Permalink | Advertise | Privacy | Mobile Web01 | 2.6.130704.1 | Last Updated 9 Feb 2013
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
14/14