Professional Documents
Culture Documents
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,974,409 members (62,842 online)
highlights off
Sign in
home
articles
quick answers
discussions
features
community
help
Article Browse Code Stats Revisions (7) Alternatives Comments & Discussions (146)
About Article
ASP.NET application and page life cycle Type Licence Article CPOL 17 Apr 2010 478,154 6,369 564 times
Prize winner in Competition "Best ASP.NET article of April 2010" Download source code - 4.03 KB
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?
ASP.NET Dev
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.
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
1/17
7/18/13
Top News
The History of CTRL + ALT + DELETE
Get the Insider News free each morning.
Related Videos
In the coming sections, we will understand both these main steps in more detail.
Related Articles
Understanding ASP.NET Application and Page Life Cycle - A Beginner's Tutorial 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 Six common uses of the Template Design Pattern: Design Pattern series Why(s) & How(s) of Asp.Net MVC Part 1 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
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
2/17
7/18/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/17
7/18/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 eand 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 t event 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 R to 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 e and 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/17
7/18/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.
I n i t
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle 5/17
7/18/13
P a g e
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/17
7/18/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/17
7/18/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/17
7/18/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/17
7/18/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/17
7/18/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
31
Excellent
Vote
ukbhatt
15-Jul-13 15:32
SK you are my favorite person in IT Industry with Andy Olsen. You really have panache and detail knowledge
Sign In View Thread Permalink
My vote of 5
Excellent
Sign In View Thread Permalink
SagarRS
28-Jun-13 2:47
What tools or software do you use to edit the pictures in your article
Dream8Lee
17-Jun-13 23: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.
Sign In View Thread Permalink
Re: What tools or software do you use to edit the pictures in your article
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
Shivprasad koirala
17-Jun-13 23:48
11/17
7/18/13
Re: What tools or software do you use to edit the pictures in your article
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.
Sign In View Thread Permalink
Sandeep Akhare
12-Jun-13 7: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
Sign In View Thread Permalink
Naufel Basheer
5-Jun-13 21:59
Nice Job
Thanks for this article it's very helpful, you open my eyes ASP.NET lifecycle.
Erik Ochoa
23-May-13 6:14
My vote of 5
excellent article,,so simple and clear ,, Thanks
Sign In View Thread Permalink
Mithileshchandan
15-May-13 20:52
My vote of 5
umeryounas
24-Apr-13 21:23
My vote of 5
Very nice graphic illustrations and good content. 5.
Sign In View Thread Permalink
GregoryW
22-Apr-13 0:01
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
12/17
7/18/13
Shripati Nirmalaa
17-Apr-13 22:35
Hi Shiv, I found this article very useful for intermediate level developers. Always been expecting new articles. Best of luck and keep rocking.
Sign In View Thread Permalink
My vote of 5
such great great article
Sign In View Thread Permalink
harshavardhanreddy
5-Apr-13 0:42
My vote of 5
Nice article. Easy to understand.. thank you ....
Sign In View Thread Permalink
mahesh27
25-Mar-13 18:11
My vote of 5
Nice article. very clarifying.
Sign In View Thread Permalink
Vick_Web
22-Mar-13 0:42
My vote of 5
great
Sign In View Thread Permalink
5-Mar-13 7:53
My vote of 4
Good Work
Sign In View Thread Permalink
Naseer A Khan
2-Mar-13 0:51
My vote of 4
Nice article and some nice diagrams!
Sign In View Thread Permalink
Reonekot
19-Feb-13 9: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 11:44
cause the one that you've shared in this page is part3.. am wondering where the part2 is?
Sign In View Thread Permalink 5.00/5 (1 vote) wbbman 16-Apr-13 10: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?
Sign In View Thread Permalink
My vote of 5
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
SleepyCrat
13-Feb-13 6:05
13/17
7/18/13
Nice article, but have you considered adding the life cycle events when 1 or more master pages are introduced? It certainly changes things a bit.
Sign In View Thread Permalink 5.00/5 (1 vote) nallapula 11-Feb-13 20:29
My vote of 5
excellent
Sign In View Thread Permalink
My vote of 3
Good one
Sign In View Thread Permalink
My vote of 5
great...
Sign In View Thread Permalink
10-Feb-13 20:31
My vote of 5
Best of the Best
Sign In View Thread Permalink
My vote of 5
excellent!
Sign In View Thread Permalink
My vote of 5
very nice representation to showcase the article
Sign In View Thread Permalink
talktogurav
20-Jan-13 23:11
My vote of 5
Well done.. Great! Keep it up.
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
14/17
7/18/13
Sign In View Thread Permalink
nice article
very nice article, describing in brief.
Sign In View Thread Permalink
Member 8887900
10-Jan-13 2:18
My vote of 4
helpful
Sign In View Thread Permalink
Abhisheik yk
10-Jan-13 0:19
etechpulse
8-Jan-13 20:01
I n t r o d u c t i o n : A p p l i c a t i o no b j e c ti su s e dt os t o r et h ev a l u e sa n da c c e s sv a r i a b l e sf r o ma n yp a g ei n a p p l i c a t i o n .I ti ss a m ea ss e s s i o no b j e c to n l yt h ed i f f e r e n c ei ss e s s i o no b j e c ti s u s e dt om a i n t a i nt h es e s s i o nf o rp a r t i c u l a ru s e r .I fo n eu s e re n t e r si nt ot h e a p p l i c a t i o nt h e ns e s s i o ni dw i l lc r e a t ef o rt h a tp a r t i c u l a ru s e ri fh el e a v e sf r o m t h ea p p l i c a t i o nt h e nt h es e s s i o ni dw i l ld e l e t e d . I ft h e ya g a i nl o gi nt ot h ew e ba p p l i c a t i o nt h e yw i l lg e td i f f e r e n ts e s s i o ni db u t a p p l i c a t i o no b j e c ti ss a m ef o ra l lu s e r so n c ea p p l i c a t i o no b j e c ti sc r e a t e dt h a t a p p l i c a t i o no b j e c ti su s e dt h r o u g h o u tt h ea p p l i c a t i o nr e g a r d l e s so fu s e r .T h ev a l u e s s t o r e di na p p l i c a t i o no b j e c ta c c e s s e dt h r o u g h o u ta l lt h ep a g e si na p p l i c a t i o na n dw e c a nc h a n g et h ea p p l i c a t i o no b j e c ti no n ep l a c et h o s ec h a n g e sa u t o m a t i c a l l yr e f l e c t e d i na l lt h ep a g e s . Y o uc a nc r e a t ea p p l i c a t i o no b j e c t si nG l o b a l . a s a xf i l ea n da c c e s st h o s ev a r i a b l e s t h r o u g h o u tt h ea p p l i c a t i o n .T ok n o wa b o u th o wt oa d dG l o b a l . a s a xf i l ec h e c kt h ep o s t h e r e : R i g h tc l i c ko np r o j e c t>A d dN e wI t e m>S e l e c tG l o b a lA p p l i c a t i o nc l a s s .( N o t e : y o uc a nh a v eo n l yo n eg l o b a l . a s a xf i l ep e rp r o j e c t )
good articale
i am really satisfied by your articale. this is very helpful for understand asp.net page life cycle..
Sign In View Thread Permalink
Good Article
Good Article
Sign In View Thread Permalink
Jayesh Goyani
18-Dec-12 2:27
Best tutorial
This is the best tutorial i have ever read Thanks, prasad
Indraprasad23
5-Dec-12 0:40
Comments
Member 8941289
4-Dec-12 2:59
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
15/17
7/18/13
My vote of 5
Great! Easy to understand! Thanks a lot!
Sign In View Thread Permalink
Member 8687858
23-Nov-12 0:30
My vote of 5
best article
Sign In View Thread Permalink
narinder singh1
18-Oct-12 7:03
My vote of 5
dude...ur article is very nice...keep doing good work..
Sign In View Thread Permalink
kavittrivedi
18-Oct-12 0:56
My vote of 5
Member 7967542
17-Oct-12 18:37
I was searching this type of article on plc for a long time. But found here. awesome article.
Sign In View Thread Permalink
My vote of 5
THX allot
Sign In View Thread Permalink
Nik0la
8-Oct-12 22:02
My vote of 5
garima14
21-Sep-12 1:04
Really surprised to see this article......one of my best trainer given exactly same content and pictures in his training program.
Sign In View Thread Permalink
My vote of 5
aditya_bokade
21-Sep-12 0:25
aah i did't notice that its shivprasad koirala..a well reputed personality!
Sign In View Thread Permalink
My vote of 5
Its so clear, and so detailed as well!
Sign In View Thread Permalink
aditya_bokade
19-Sep-12 0:31
My vote of 5
abhishekgour
16-Sep-12 5:56
Nice presentation of topic as well as good trick to understand and memorize it for life time.
Sign In View Thread Permalink
My vote of 5
Excellent Artical !!!! Really Enjoyed thanks !!!!!!!
Sign In View Thread Permalink
ahp-1984
14-Sep-12 2:25
My vote of 5
aryaverman188
4-Aug-12 2:35
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
16/17
7/18/13
gr8
Sign In View Thread Permalink
my +5..
very helpful article..
Sign In View Thread Permalink
Rajesh Biswas
28-Jul-12 1:32
My vote of 5
Compact & smooth....
Sign In View Thread Permalink
sanndeb
20-Jul-12 0:02
1 2 3 Next Admin
Article Copyright 2010 by Shivprasad koirala Everything else Copyright CodeProject, 1999-2013 Terms of Use
Permalink | Advertise | Privacy | Mobile Web02 | 2.6.130714.1 | Last Updated 9 Feb 2013
www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
17/17