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:
- Application life cycle
- Page life cycle
ASP.NET Application life cycle
The 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 take places:
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 are compiled.
3. Response objects are created. The application objects such as HttpContext, HttpRequest and HttpResponse are created and initialized.
4. An instance of the HttpApplication object is created and assigned to the request.
5. The request is processed by the HttpApplication class. Different events are raised by this class for processing the request.
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.
Following are the different stages of an ASP.NET Page:
Page_Init Page initialization
LoadViewState View State Loading
LoadPostData Postback Data processing
Page_load Page loading
RaisePostDataChangeEvent PostBack change Notification
RaisePostBackEvent PostBack event Handling
Page_PreRender Page Pre Rendering phase
SaveViewState View state Saving
Page_Render Page Rendering
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.
Preload: Preload 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 page_LoadComplete handler.
PreRender: The 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.