1. ASP.NETlife cycle specifies,how:
ASP.NETprocessespagestoproduce dynamicoutput
The applicationanditspagesare instantiatedandprocessed
ASP.NETcompilesthe pagesdynamically
The ASP.NETlife cycle couldbe dividedintotwogroups:
ApplicationLife Cycle
Page Life Cycle
ASP.NETApplicationLifeCycle
ASP.NET Page Life Cycle
Whena page isrequested,itisloadedintothe servermemory,processed,andsenttothe browser.
Thenit isunloadedfromthe memory.Ateachof these steps,methodsandeventsare available,which
couldbe overriddenaccordingtothe needof the application.Inotherwords,youcanwrite yourown
code to override the defaultcode.
2. The Page classcreatesa hierarchical tree of all the controlson the page.All the componentsonthe
page,exceptthe directives,are partof thiscontrol tree.You can see the control tree by addingtrace=
"true"to the page directive.We will coverpage directivesandtracingunder'directives'and'event
handling'.
The page life cycle phasesare:
Initialization
Instantiationof the controlsonthe page
Restorationandmaintenance of the state
Executionof the eventhandlercodes
Page rendering
Understandingthe page cycle helpsinwritingcodesformakingsome specificthinghappenatanystage
of the page life cycle.Italsohelpsinwritingcustomcontrolsandinitializingthematrighttime,populate
theirpropertieswithview-statedataandrun control behaviorcode.
Followingare the differentstagesof anASP.NETpage:
Page request - WhenASP.NETgetsa page request,itdecideswhethertoparse and
compile the page,orthere wouldbe a cachedversionof the page;accordinglythe response issent.
Starting of page life cycle - At thisstage,the RequestandResponse objectsare set.
If the requestisanoldrequestor postback, the IsPostBackpropertyof the page is setto true.The
UICulture propertyof the page isalso set.
Page initialization - At thisstage,the controls on the page are assigneduniqueIDby
settingthe UniqueIDpropertyandthe themesare applied.Foranew request,postbackdatais loaded
and the control propertiesare restoredtothe view-statevalues.
3. Page load - At thisstage,control propertiesare setusingthe view state andcontrol state
values.
Validation - Validate methodof the validationcontrol iscalledandonitssuccessful execution,
the IsValidpropertyof the page issetto true.
Postback event handling - If the requestisa postback (oldrequest),the related
eventhandlerisinvoked.
Page rendering - At thisstage,view state forthe page and all controlsare saved.The page
callsthe Rendermethodforeachcontrol and the outputof renderingiswrittentothe OutputStream
classof the Response propertyof page.
Unload - The renderedpage issenttothe clientandpage properties,suchasResponse and
Request,are unloadedandall cleanupdone.
ASP.NET Page Life Cycle Events
At eachstage of the page life cycle,the page raisessome events,whichcouldbe coded.Anevent
handlerisbasicallyafunctionorsubroutine,boundtothe event,usingdeclarative attributessuchas
Onclickor handle.
Followingare the page life cycle events:
PreInit- PreInitisthe firsteventinpage life cycle.Itchecksthe IsPostBackpropertyanddetermines
whetherthe page isa postback.It setsthe themesandmasterpages,createsdynamiccontrols,andgets
and setsprofile propertyvalues.Thiseventcanbe handledbyoverloadingthe OnPreInitmethodor
creatinga Page_PreInithandler.
4. Init- Initeventinitializesthe control propertyandthe control tree isbuilt.Thiseventcanbe handledby
overloadingthe OnInitmethodorcreatingaPage_Inithandler.
InitComplete - InitComplete eventallowstrackingof view state.All the controlsturnonview-state
tracking.
LoadViewState- LoadViewState eventallowsloadingview state informationintothe controls.
LoadPostData- Duringthisphase,the contentsof all the inputfieldsare definedwiththe <form>tagare
processed.
PreLoad- PreLoadoccurs before the postbackdata is loadedinthe controls.Thiseventcanbe handled
by overloadingthe OnPreLoadmethodorcreatingaPage_PreLoadhandler.
Load - The Load eventisraisedforthe page firstandthenrecursivelyforall childcontrols.The controls
inthe control tree are created.Thiseventcan be handledbyoverloadingthe OnLoadmethodor
creatinga Page_Loadhandler.
LoadComplete - The loadingprocessiscompleted,control eventhandlersare run,andpage validation
takesplace.Thiseventcanbe handledbyoverloadingthe OnLoadComplete methodorcreatinga
Page_LoadComplete handler
PreRender- The PreRendereventoccursjustbefore the outputisrendered.Byhandlingthisevent,
pagesand controlscan performanyupdatesbefore the outputisrendered.
PreRenderComplete- Asthe PreRendereventisrecursivelyfiredforall childcontrols,thiseventensures
the completionof the pre-renderingphase.
SaveStateComplete - State of control onthe page issaved.Personalization,control state andview state
informationissaved.The HTML markupis generated.Thisstage canbe handledbyoverridingthe
Rendermethodorcreatinga Page_Renderhandler.
5. UnLoad - The UnLoad phase is the lastphase of the page life cycle.Itraisesthe UnLoad eventforall
controlsrecursivelyandlastlyforthe page itself.Final cleanupisdone andall resourcesandreferences,
such as database connections,are freed.Thiseventcanbe handledbymodifyingthe OnUnLoadmethod
or creatinga Page_UnLoad handler.