Tuesday, September 18, 2012

Explain the life cycle of an ASP .NET page

ASP.NET 2.0 Page Life Cycle - The lifetime of an ASP.NET page is filled with events. A .NET technical interview might begin with this question. A series of processing steps takes place during this page life cycle. Following tasks are performed:
  • Initialization
  • Instantiation of controls
  • Restoration & Maintainance of State
  • Running Event Handlers
  • Rendering of data to the browser
The life cycle may be broken down into Stages and Events. The stages reflect the broad spectrum of tasks performed. The following stages take place
  1. Page Request - This is the first stage, before the page life cycle starts. Whenever a page is requested, ASP.NET detects whether the page is to be requested, parsed and compiled or whether the page can be cached from the system.
  2. Start - In this stage, properties such as Request and Response are set. Its also determined at this stage whether the request is a new request or old, and thus it sets the IsPostBack property in the Start stage of the page life cycle.
  3. Page Initialization - Each control of the page is assigned a unique identification ID. If there are themes, they are applied. Note that during the Page Initialization stage, neither postback data is loaded, nor any viewstate data is retrieved.
  4. Load - If current request is a postback, then control values are retrieved from their viewstate.
  5. Validation - The validate method of the validation controls is invoked. This sets the IsValid property of the validation control.
  6. PostBack Event Handling - Event handlers are invoked, in case the request is a postback.
  7. Rendering - Viewstate for the page is saved. Then render method for each control is called. A textwriter writes the output of the rendering stage to the output stream of the page's Response property.
  8. Unload - This is the last stage in the page life cycle stages. It is invoked when the page is completely rendered. Page properties like Respone and Request are unloaded.

No comments: