Sunday, January 11, 2009

Page life cycle events

At every stage of its lifecycle, the page raises following events:

Stage PageInitialization - Events raised PreInit,Init,InitComplete

  • PreInit - handles following tasks:
  1. The IsPostBack property is checked to determine if the page is being processed for the first time .
  2. Dynamic controls are created
  3. A master page is dynamically.
  4. Theme property is set dynamically.
  5. Profile property values are read or set.

    Note : The values of the controls are not restored from viewstate yet if it's a postback. so if any control property is set during this event, its value might be overwritted in the next event.
  • Init -
  1. Control properties are read or initialized.
  2. It's triggered after the controls are initialized and skin settings are applied.
  • Initcomplete -
  1. Those tasks that need the initialization to be complete are processed.
  2. Page object triggers it

Stage LOAD - Events raised Preload, Load

  • PreLoad -
  1. Once this event is triggered by Page, the view state of the page and all controls are loaded and any postback data included in the Request instance is processed.
  • Load -
  1. Properties of the controls are set using OnLoad event.
  2. Database connections are established.
  3. The OnLoad event method of the page and its child control are called recursively until they all are loaded.
  • Control events -
  1. Control specific events are handled at this stage. e.g. TextChanged event’s for a textbox.
  2. In a postback request, if the page contains validator controls, you should check the IsValid property of the page and of the individual validation controls before performing any processing.

  • LoadComplete -
  1. For accessing controls that have been properly loaded.
  • PreRender
  1. To make any final changes to the controls before they are rendered.
  2. Before this event , the Page object calls EnsureChildControls for each control and for the page. Each data bound control, whose DataSourceID property is set, calls its DataBind method.
  • SaveStateComplete-
  1. Used for tasksthat require view state to be saved, yet not making any changes to the controls themselves.
  2. ViewState for the page and its controls are saved before this event occurs and thus any chhanges to the page or controls at this point will be ignored.
  • Render
  1. The page object calls this method on all of the controls, so all the controls are written and sent to the browser.

Note: If a custom control incorporates only standard NET Web server controls and no custom markup, you do not need to override the Render method which you do normally for any custom control.

  • Unload -
  1. A final cleanup for specific controls such as closing the control-specific database connections and open files, finishing up logging is performed here.
  2. The Unload event occurs for each control, and then for the page.

    Note : No further changes to the response stream can be made now. All the controls UnLoad method are fired, followed by the pages UnLoad event (bottom-up). This stage is used for closing database connections, closing open files, etc.
  • The Init, Unload event for each child control occur before its parent container corresponding event are raised. However the Load event for a container occurs before the Load events for its child controls (top-down).

Source of Reference: MSDN

No comments:

 
Google