ASP.NET Application and Page life Cycle
This blog talks about the ASP.NET application life cycle and
page life cycle.
The ASP.NET life cycle could be divided into two groups:
ASP.NET Application life cycle
application life cycle has the following stages:
1. User makes a request for accessing
application resource, a page. Browser sends this request to the web server.
2. A unified pipeline receives the first request and the following events
a) An object of the class
ApplicationManager is created.
b) An object of the class Hosting
Environment is create to provide information regqarding the resources.
c) Top level items in the application
3. Response objects are created. The
application objects such as HttpContext, HttpRequest and HttpResponse are
created and initialized.
instance of the HttpApplication
object is created and assigned to the
5. The request is processed by the
HttpApplication class. Different events are
raised by this class for processing the
ASP.NET Page life cycle
When a page
is requested, it is loaded into the server memory, processed,and sent to the browser. Then it is unloaded from the
memory. At each of these steps, methods and events are variable,which could be
overridden according to the need of the application.
the different stages of an ASP.NET Page:
View State Loading
LoadPostData Postback Data processing
RaisePostDataChangeEvent PostBack change
PostBack event Handling
Page Pre Rendering phase
View state Saving
Page_Unload Page unloading
PreInit: PreInit is the first event
in page life cycle. It checks the postback property and determines whether the
page is a postback. It sets the
themes and master pages ,creates dynamic control, and gets and sets profiles
property values. This event can be handled by overloading the OnPreInit() method or creating a page_Preinit
method or creating a Page_PreInit handler.
Init: Init event
initializes the control property
and the control tree is build. This event can be
handled by overloading the OnInit()
method or creating a Page_Init handler.
InitComplete: InitComplete event allows tracking of view state. All the controls turn on view-state tracking.
LoadViewState: LoadViewState event allows loading view state information
into the controls.
LoadPostData: During this phase, the
contents of all the input fields are defined with the<form> tag are processed.
occurs before the post back data is loaded in the controls. This event can be handled by overloading the onPreLoad method or creating a Page_preLoad handler.
LoadComplete: The loading process is completed, control event handlers or
run, and page validation takes place. This event can be handled by overloading
the OnLoadComplete method or creating a
PreRender event occurs just before the output is rendered. By handling this
event, pages and controls can perform any updates before the output is rendered.
PreRenderComplete: As the preRender event is recursively fired for all child
control, this event ensures the
completion of the pre-rendering phase.
SaveStateComplete: State of control on the page is saved. Personalization,
control state and view state information is saved. The HTML markup is
generated. This stage can be handled by
overriding the Render() method or creating a Page_Render Handler.
Unload: The unload phase is the last phase of the page life cycle. It raises the Unload event for all controls recursively and
lastly for the page itselfe. Final cleanup is done and all resources and
references, such as database connections are freed. This event can be handled by modified the OnUnload()
method or creating a page_Unload handler.