Sep 19

asp dotnet page life cycle

In asp.net page goes through a life cycle which perform step by step. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. The life cycle of a control is based on the page life cycle, but the page raises more events for a control than are available for an ASP.NET page alone.

Page Life Cycle Stage

  1. Page Request : The page request occurs before the page life cycle begins. When the page is requested by a user, ASP.NET determines whether the page needs to be parsed and compiled, or whether a cached version of the page can be sent in response without running the page.
  2. Starting of Page Life Cycle : At this position.  The request and response object are set. the page also determines whether the request is a postback or a new request and sets the IsPostBack property. The page also sets the UICulture property.
  3. Page Initialization :  During page initialization, controls on the page are available and each control’s UniqueID property and themes are applied.  For a new request postback data is loaded and the control properties are restored to the view-state values.
  4. Page Load : During load, if the current request is a postback, control properties are loaded with information recovered from view state and control 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  request is postback, the related event handler is called.
  7. Page Rendering : Before rendering, view state is saved for the page and all controls. The page calls the Render method for each control and the output of rendering is written to the OutputStream class of the Page’s Response property.
  8. Unload : Unload is called after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response andRequest are unloaded and any cleanup is performed.


Life Cycle Event

Within each stage of the life cycle of a page, the page raises events that you can handle to run your own code. For control events, you bind the event handler to the event, either declaratively using attributes such as onclick, or in code. Page also support event wire-up. meaning that ASP.NET looks for methods with particular names and automatically runs those methods when certain events are raised.

List of Page Life Cycle Event

  1. PreInit : PreInit event check the IsPostback Property to determine whether this is the first time the page is being processed. Create or re-create dynamic controls. Set a master page dynamically. Set the theme property dynamically. Read or set profile property value.
  2. Init : Raised after all controls have been initialized and any skin settings have been applied. Use this event to read or initialize control properties.
  3. InitComplete : Raised by the Page object. Use this event for processing tasks that require all initialization be complete.
  4. 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.
  5. Load : The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded.
  6. Control Event : use these events to handle specific control events. Such as a button control’s, Click Event or a textbox control’s TextChanged event.
  7. Load Complete : Use this event for tasks that require that all other controls on the page be loaded.
  8. PreRender : before this event the page object call Ensurechildcontrol for each control and for the page. The PreRender event occurs for each control on the page. Use the event to make final changes to the contents of the page or its controls.
  9. Savestatecomplete: before this event view state has been saved for the page and for all controls. Any changes to the page and control at this point will be ignored. Use this event perform tasks that require view state to be saved, but that do not make any changes to controls.
  10. Render : The page object call this method on each control. All asp.net web server controls have render method that writes out the control’s markup that is sent to the browser.
  11. Unload : This event occurs for each control and then for the page. In controls, use this event to do final cleanup for specific controls, such as closing control-specific database connections.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>