Friday, May 18, 2012

Page Life Cycle in ASP.NET

In this post, we will understand the Page Life Cycle in ASp.Net. It is very important to understand the Page Life Cycle in Asp.Net for many reasons, mainly for understanding where we can place particular methods and set page properties efficiently.

 If you are a fresher even experienced then you will see that this is a very common question in the interviews that “Explain the Page Life Cycle of Asp.Net?”

So here I am trying to explain the Page Life Cycle in simple way .I hope this will help you to understand this.


Before I start the explanation of Page Life Cycle, here I explain some important resources which uses on page request:-

Internet Information Server (IIS) - IIS is the default web server in Microsoft .Net. It helps to deploy web sites/ web applications. IIS receives request for a web resource (file) and check the extension of the file (.aspx, .ashx, .asmx etc) and determines which HTTP handler will handle this request and send the request to proper handlers.

ASPNET_ISAPI.DLL- IIS loads this DLL and send request to this DLL. This DLL loads the HTTP Runtime for further processing.

ASPNET_WP.EXE- It contains an application pool. Each Application Pool can contain any number of Applications. Application Pool is also called as AppDomain. When a web page is requested, IIS looks for the application pool under which the current application is running and forwards the request to the respective worker process.

Page Life Cycle

When an ASP.Net page runs, it goes through several processing steps which called the Page Life Cycle in simple words. Mainly Page life depends on the fact that whether the page is requested first time or after the Post back (page request of itself).

Page Life Cycle Stages

I general, when a user request a page through his/her browser then page goes through several stage outlined in the following list. In addition to the page life-cycle stages, there are application stages that occur before and after a request but are not specific to a page. The stages are:-
  1. Page Request: - Page Request occurs before page life cycle begins. When user requested the page, ASP.Net determines that the page is needed to be parsed and compiled or whether a cached version of the page can be sent in response without running the page.
  2. Start: - In the start stage, page properties such as Request and Response is set. At this stage, page also determines that request is post back or a new request and sets the IsPostBack property.
  3. Page Initialization: - During page initialization, controls on the page are available and each control’s UniqueID property is set. A master page and themes are also applied to the page if applicable. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.
  4.  Load: - During Load, if the current request is postback, control properties are loaded with information recovered from view state.
  5. Validation: - During validation, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.
  6. Postback Event Handling: - If the request is a postback, any event handlers are called. The event handling for server controls occurs during this stage.
  7. Rendering Unload: - Before rendering, view state is saved for the page and all controls. During the rendering stage, the page calls the Render method for each control, providing a text writer that writes its output to the OutPutStream object of the page's Response property.

Page Life Cycle Events

Within each stage of page life cycle, page raises several events that we can handle to run our own code. Pages also support automatic event wire-up, meaning that ASP.NET looks for methods with particular names and automatically runs those methods when certain events are raised. These events are:-
  1. PreInit: - Raised after the start stage complete and before initialization stage begins.
  2. Init:- Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before Init event of page.
  3. InitComplete: - Raised at the end of the initialization stage.
  4.  PreLoad: - Raised after the page loads view state of itself and all controls
  5. Load: - OnLoad event is occurred during the Load stage.
  6. Control Events: - Use these events to handle specific control events, such as Button’s Onclick event.
  7. LoadComplete: - Raised at the end of the event-handling stage.
  8. PreRender: - Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls.
  9. PreenderComplete: - Raised after each data bound control whose DataSourceID property is set calls its DataBind method.
  10.  SaveStateComplete: - Raised after view state and control state have been saved for the page and for all controls. 
  11.  Render: - This is not an event; instead, at this stage of processing, the Page object calls this method on each control. All ASP.NET Web server controls have a Render method that writes out the control's markup to send to the browser.
  12.  Unload: - Raised for each control and then for the page.

Refrences: -

No comments:

Post a Comment

If You Enjoyed This Post Please Take 5 Seconds To Share It.

^ Scroll to Top hgpromo