Professional Documents
Culture Documents
home
articles
quick answers
discussions
features
community
help
Next
Article Browse Code Bugs / Suggestions Stats Revisions (2) Alternatives Comments (1)
About Article
When a page request is sent to the Web server, the page is run through a series of events during its creation and disposal. In this article, I will Type Licence First Posted Views Bookmarked Article CPOL 11 Oct 2013 8,828 2 times
Editorial Note
This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.
Share
When a page request is sent to the Web server, the page is run through a series of events during its creation and disposal. In this article, I will discuss in detail the ASP.NET page life cycle Events
( 1 )P r e I n i tT h ee n t r yp o i n to ft h ep a g el i f ec y c l ei st h ep r e i n i t i a l i z a t i o np h a s ec a l l e d P r e I n i t .T h i si st h eo n l ye v e n tw h e r ep r o g r a m m a t i ca c c e s st om a s t e rp a g e sa n dt h e m e si s a l l o w e d .Y o uc a nd y n a m i c a l l ys e tt h ev a l u e so fm a s t e rp a g e sa n dt h e m e si nt h i se v e n t .Y o u c a na l s od y n a m i c a l l yc r e a t ec o n t r o l si nt h i se v e n t . E X A M P L E:O v e r r i d et h ee v e n ta sg i v e nb e l o wi ny o u rc o d e b e h i n dc sf i l eo fy o u ra s p xp a g e
u s i n gS y s t e m ; u s i n gS y s t e m . C o l l e c t i o n s . G e n e r i c ; u s i n gS y s t e m . L i n q ; u s i n gS y s t e m . W e b ; u s i n g S y s t e m . W e b . U I ; u s i n gS y s t e m . W e b . U I . W e b C o n t r o l s ;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 _ P r 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 ) { / / U s et h i se v e n tf o rt h ef o l l o w i n g : / / C h e c kt h eI s P o s t B a c kp r o p e r t yt o d e t e r m i n ew h e t h e rt h i si st h ef i r s tt i m et h ep a g ei sb e i n gp r o c e s s e d . / / C r e a t e o rr e c r e a t ed y n a m i cc o n t r o l s . / / S e tam a s t e rp a g ed y n a m i c a l l y . / / S e t t h eT h e m ep r o p e r t yd y n a m i c a l l y . }
( 2 ) I n i tT h i se v e n tf i r e sa f t e re a c hc o n t r o lh a sb e e ni n i t i a l i z e d ,e a c hc o n t r o l ' sU n i q u e I D i ss e ta n da n ys k i ns e t t i n g sh a v eb e e na p p l i e d .Y o uc a nu s et h i se v e n tt oc h a n g e i n i t i a l i z a t i o nv a l u e sf o rc o n t r o l s .T h e I n i t e v e n ti sf i r e df i r s tf o rt h em o s tb o t t o m c o n t r o li nt h eh i e r a r c h y ,a n dt h e nf i r e du pt h eh i e r a r c h yu n t i li ti sf i r e df o rt h ep a g e i t s e l f . E X A M P L E:O v e r r i d et h ee v e n ta sg i v e nb e l o wi ny o u rc o d e b e h i n dc sf i l eo fy o u ra s p xp 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 ) { / /R a i s e da f t e ra l lc o n t r o l sh a v eb e e ni n i t i a l i z e da n da n ys k i ns e t t i n g sh a v eb e e n a p p l i e d .U s et h i se v e n tt or e a do ri n i t i a l i z ec o n t r o lp r o p e r t i e s . }
( 3 ) I n i t C o m p l e t eR a i s e do n c ea l li n i t i a l i z a t i o n so ft h ep a g ea n di t sc o n t r o l sh a v eb e e n c o m p l e t e d .T i l ln o wt h ev i e w s t a t ev a l u e sa r en o ty e tl o a d e d ,h e n c ey o uc a nu s et h i se v e n t t om a k ec h a n g e st ov i e ws t a t et h a ty o uw a n tt om a k es u r ea r ep e r s i s t e da f t e rt h en e x t p o s t b a c k E X A M P L E:O v e r r i d et h ee v e n ta sg i v e nb e l o wi ny o u rc o d e b e h i n dc sf i l eo fy o u ra s p xp a g e
p r o t e c t e dv o i dP a g e _ I n i t C o m p l e t e ( o b j e c ts e n d e r ,E v e n t A r g se ) { / /R a i s e db yt h e P a g eo b j e c t .U s et h i se v e n tf o rp r o c e s s i n gt a s k st h a tr e q u i r e a l li n i t i a l i z a t i o nb ec o m p l e t e .}
( 4 ) P r e L o a dR a i s e da f t e rt h ep a g el o a d sv i e ws t a t ef o ri t s e l fa n da l lc o n t r o l s ,a n da f t e ri t p r o c e s s e sp o s t b a c kd a t at h a ti si n c l u d e dw i t ht h eR e q u e s ti n s t a n c e ( 1 ) L o a d sV i e w S t a t e:V i e w S t a t ed a t aa r el o a d e dt oc o n t r o l s N o t e:T h ep a g ev i e w s t a t ei sm a n a g e db yA S P . N E Ta n di su s e dt op e r s i s ti n f o r m a t i o no v e ra
Top News
C# 6: First reactions
Get the Insider News free each
http://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cycle-Events
1/4
4/11/2014
Related Videos
p r o t e c t e do v e r r i d ev o i dO n P r e L o a d ( E v e n t A r g se ) { / /U s et h i se v e n ti fy o un e e dt op e r f o r mp r o c e s s i n go ny o u rp a g eo rc o n t r o l b e f o r et h e L o a de v e n t . / /B e f o r et h eP a g ei n s t a n c er a i s e st h i se v e n t ,i tl o a d s v i e ws t a t ef o ri t s e l fa n da l lc o n t r o l s ,a n dt h e np r o c e s s e sa n yp o s t b a c kd a t ai n c l u d e dw i t h t h eR e q u e s ti n s t a n c e . }
( 5 ) L o a dT h ei m p o r t a n tt h i n gt on o t ea b o u tt h i se v e n ti st h ef a c tt h a tb yn o w ,t h ep a g eh a s
Related Articles
Understanding ASP.NET Application and Page Life Cycle - A Beginner's Tutorial [Explained]Life Cycle of an ASP.NET Page ASP.NET Page Life Cycle Events The ASP.NET Page Lifecycle A Basic Approach ASP.NET Life Cycle Overview ASP.NET Application and Page Life Cycle ASP.NET Page Life Cycle How to skip calling Page_Load event Callback WebControls The Page Life Cycle of Client [Browser] Why not to pass endResponse parameter as true in Response.Redirect method Client Callbacks in ASP.NET 2.0 Introducing ASP.NET Page Modules ASP.NET Web Form Model with Partial Rendering and Events 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() Classic ASP Framework 2.0 Make your Classic ASP code work like in ASP.NET MVC Introduction ViewState and Server.Transfer Best practices ViewState and Postback
b e e nr e s t o r e dt oi t sp r e v i o u ss t a t ei nc a s eo fp o s t b a c k s .C o d ei n s i d et h ep a g el o a de v e n t t y p i c a l l yc h e c k sf o rP o s t B a c ka n dt h e ns e t sc o n t r o lp r o p e r t i e sa p p r o p r i a t e l y .T h i sm e t h o d i st y p i c a l l yu s e df o rm o s tc o d e ,s i n c et h i si st h ef i r s tp l a c ei nt h ep a g el i f e c y c l et h a t a l lv a l u e sa r er e s t o r e d .M o s tc o d ec h e c k st h ev a l u eo fI s P o s t B a c kt oa v o i du n n e c e s s a r i l y r e s e t t i n gs t a t e .Y o um a ya l s ow i s ht oc a l lV a l i d a t ea n dc h e c kt h ev a l u eo fI s V a l i di nt h i s m e t h o d .Y o uc a na l s oc r e a t ed y n a m i cc o n t r o l si nt h i sm e t h o d .
E X A M P L E:O v e r r i d et h ee v e n ta sg i v e nb e l o wi ny o u rc o d e b e h i n dc sf i l eo fy o u ra s p xp a g e
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 ) { / /T h e P a g ec a l l st h e O n L o a de v e n tm e t h o do nt h e P a g e ,t h e nr e c u r s i v e l yd o e s t h es a m ef o re a c hc h i l dc o n t r o l ,w h i c hd o e st h es a m ef o re a c ho fi t sc h i l dc o n t r o l su n t i l t h ep a g ea n da l lc o n t r o l sa r el o a d e d . / /U s et h eO n L o a de v e n tm e t h o dt os e t p r o p e r t i e si nc o n t r o l sa n de s t a b l i s hd a t a b a s ec o n n e c t i o n s . }
( 6 ) C o n t r o l( P o s t B a c k )e v e n t ( s ) A S P . N E Tn o wc a l l sa n ye v e n t so nt h ep a g eo ri t sc o n t r o l st h a t c a u s e dt h eP o s t B a c kt oo c c u r .T h i sm i g h tb eab u t t o n sc l i c ke v e n to rad r o p d o w n ' s s e l e c t e d i n d e x c h a n g ee v e n t ,f o re x a m p l e . T h e s ea r et h ee v e n t s ,t h ec o d ef o rw h i c hi sw r i t t e n i ny o u rc o d e b e h i n dc l a s s ( . c sf i l e ) . E X A M P L E:O v e r r i d et h ee v e n ta sg i v e nb e l o wi ny o u rc o d e b e h i n dc sf i l eo fy o u ra s p xp a g 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 ) { / /T h i si sj u s ta ne x a m p l eo fc o n t r o le v e n t . .H e r ei ti sb u t t o nc l i c ke v e n tt h a t c a u s e dt h ep o s t b a c k }
( 7 ) L o a d C o m p l e t eT h i se v e n ts i g n a l st h ee n do fL o a d . E X A M P L E:O v e r r i d et h ee v e n ta sg i v e nb e l o wi ny o u rc o d e b e h i n dc sf i l eo fy o u ra s p xp a g e
p r o t e c t e dv o i dP a g e _ L o a d C o m p l e t e ( o b j e c ts e n d e r ,E v e n t A r g se ) { l o a d e d . } / /U s et h i se v e n tf o rt a s k st h a tr e q u i r et h a ta l lo t h e rc o n t r o l so nt h ep a g eb e
p r o t e c t e do v e r r i d ev o i dO n P r e R e n d e r ( E v e n t A r g se ) { / /E a c hd a t ab o u n dc o n t r o lw h o s eD a t a S o u r c e I Dp r o p e r t yi ss e tc a l l si t sD a t a B i n d m e t h o d . / /T h eP r e R e n d e re v e n to c c u r sf o re a c hc o n t r o lo nt h ep a g e .U s et h ee v e n t t om a k ef i n a lc h a n g e st ot h ec o n t e n t so ft h ep a g eo ri t sc o n t r o l s . }
( 9 ) S a v e S t a t e C o m p l e t eP r i o rt ot h i se v e n tt h ev i e ws t a t ef o rt h ep a g ea n di t sc o n t r o l si s
http://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cycle-Events
2/4
4/11/2014
p r o t e c t e do v e r r i d ev o i dO n S a v e S t a t e C o m p l e t e ( E v e n t A r g se ) { / /B e f o r et h i se v e n to c c u r s , V i e w S t a t eh a sb e e ns a v e df o rt h ep a g ea n df o ra l l c o n t r o l s .A n yc h a n g e st ot h ep a g eo rc o n t r o l sa tt h i sp o i n tw i l lb ei g n o r e d . / / U s et h i se v e n tp e r f o r mt a s k st h a tr e q u i r ev i e ws t a t et ob es a v e d ,b u tt h a td on o tm a k ea n y c h a n g e st oc o n t r o l s . }
( 1 0 ) R e n d e rT h i si sam e t h o do ft h ep a g eo b j e c ta n di t sc o n t r o l s( a n dn o ta ne v e n t ) .A tt h i s p o i n t ,A S P . N E Tc a l l st h i sm e t h o do ne a c ho ft h ep a g e sc o n t r o l st og e ti t so u t p u t .T h e R e n d e rm e t h o dg e n e r a t e st h ec l i e n t s i d eH T M L ,D y n a m i cH y p e r t e x tM a r k u pL a n g u a g e( D H T M L ) , a n ds c r i p tt h a ta r en e c e s s a r yt op r o p e r l yd i s p l a yac o n t r o la tt h eb r o w s e r . N o t e :R i g h tc l i c ko nt h ew e bp a g ed i s p l a y e da tc l i e n t ' sb r o w s e ra n dv i e wt h eP a g e ' s S o u r c e .Y o uw i l ln o tf i n da n ya s p xs e r v e rc o n t r o li nt h ec o d e .B e c a u s ea l la s p xc o n t r o l s a r ec o n v e r t e dt ot h e i rr e s p e c t i v eH T M Lr e p r e s e n t a t i o n .B r o w s e ri sc a p a b l eo fd i s p l a y i n g H T M La n dc l i e n ts i d es c r i p t s . E X A M P L E:O v e r r i d et h ee v e n ta sg i v e nb e l o wi ny o u rc o d e b e h i n dc sf i l eo fy o u ra s p xp a g e
/ /R e n d e rs t a g eg o e sh e r e .T h i si sn o ta ne v e n t
( 1 1 ) U n L o a dT h i se v e n ti su s e df o rc l e a n u pc o d e .A f t e rt h ep a g e ' sH T M Li sr e n d e r e d ,t h e o b j e c t sa r ed i s p o s e do f .D u r i n gt h i se v e n t ,y o us h o u l dd e s t r o ya n yo b j e c t so rr e f e r e n c e s y o uh a v ec r e a t e di nb u i l d i n gt h ep a g e .A tt h i sp o i n t ,a l lp r o c e s s i n gh a so c c u r r e da n di ti s s a f et od i s p o s eo fa n yr e m a i n i n go b j e c t s ,i n c l u d i n gt h eP a g eo b j e c t .C l e a n u pc a nb e p e r f o r m e do n ( a ) I n s t a n c e so fc l a s s e si . e .o b j e c t s ( b ) C l o s i n go p e n e df i l e s ( c ) C l o s i n gd a t a b a s ec o n n e c t i o n s . E X A M P L E:O v e r r i d et h ee v e n ta sg i v e nb e l o wi ny o u rc o d e b e h i n dc sf i l eo fy o u ra s p xp a g e
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 ) { / /T h i se v e n to c c u r sf o re a c hc o n t r o la n dt h e nf o rt h ep a g e .I nc o n t r o l s ,u s e t h i se v e n tt od of i n a lc l e a n u pf o rs p e c i f i cc o n t r o l s ,s u c ha sc l o s i n gc o n t r o l s p e c i f i c d a t a b a s ec o n n e c t i o n s . / /D u r i n gt h eu n l o a ds t a g e ,t h ep a g ea n di t sc o n t r o l sh a v e b e e nr e n d e r e d ,s oy o uc a n n o tm a k ef u r t h e rc h a n g e st ot h er e s p o n s es t r e a m . / / I f y o ua t t e m p tt oc a l lam e t h o ds u c ha st h eR e s p o n s e . W r i t em e t h o d ,t h ep a g ew i l lt h r o wa n e x c e p t i o n . }
F o rM o r eR e f e r e n c e:F o l l o wt h eb e l o wl i n k s( 1 ) h t t p : / / w w w . 1 5 s e c o n d s . c o m / i s s u e / 0 2 0 1 0 2 . h t m ( 2 )h t t p : / / m s d n . m i c r o s o f t . c o m / e n u s / l i b r a r y / m s 1 7 8 4 7 2 . a s p x ( 3 ) h t t p : / / w w w . d e v l i f e s t y l e . n e t / b l o g s / a r t i c l e s / a r c h i v e / 2 0 0 9 / 0 5 / 2 4 / a s p n e t i n t e r n a l s v i e w s t a t e a n d p a g e l i f e c y c l e . a s p xT h a n kY o u . . .
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
ASP.NET Community
United States The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but
http://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cycle-Events
3/4
4/11/2014
Article Top
Nice
Last Visit: 31-Dec-99 18:00 General News Last Update: 10-Apr-14 4:56 Suggestion Question Bug Answer
24-Nov-13 6:38 1
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Mobile Web03 | 2.8.140408.1 | Last Updated 11 Oct 2013
Article Copyright 2013 by ASP.NET Community Everything else Copyright CodeProject, 1999-2014 Terms of Use
http://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cycle-Events
4/4