Wednesday, 25 July 2012

Page Life Cycle Events


Page Life Cycle Events
This is a transcript of a  programming manual, to refresh my memory, and it’s always good to RTFM. As a control developer you must have a solid understanding of the page life cycle and its phases.
The page framework takes the following actions to handle a request for an .aspx file:
  1. Parses the .aspx file and creates a tree of control builders.
  2. Uses the tree to dynamically implement a class or control that derives from the Page class.
  3. Dynamically compiles the class.
  4. Caches the compiled class for later use.
  5. Dynamically creates an instance of the compiled class. The page springs to life and starts its life cycle where the page goes through different phases of its life cycle.
Page Life Cycle Phases
  1. PreInit
  2. Init
  3. InitComplete
  4. LoadControlState(Postback only)
  5. LoadViewState(Postback only)
  6. LoadPostData(Postback only – first try)
  7. PreLoad
  8. Load
  9. LoadPostData(Postback only – second try)
  10. RaisePostDataChangedEvent(Postback only)
  11. RaisePostbackEvent(Postback only)
  12. LoadComplete
  13. RaiseCallbackEvent(Postback and Callback only)
  14. PreRender
  15. PreRenderComplete
  16. SaveControlState
  17. SaveViewState
  18. SaveStateComplete
  19. Render
  • The page first retrieves the posted data from the QueryString or Form Collection of the Request object.
  • The page then checks whether the posted Data Collection (the NameValueCollection  Form or QueryString) contains an item with the key _CALLBACKID. If it does, it sets its IsCallBack Boolean property to true to signal that the page has been posted back to the server through the ASP.NET callback mechanism.
1. PreInit: The page takes the following actions in the PreInit phase of its life cycle:
a.  Calls its OnPreInit method to raise the PreInit event.
b. Initializes the theme by using the contents of the App_Themes directory to dynamically implement a class of type PageTheme, compiles the class, creates an instance of the compiled class, and assigns the instance to its PageTheme property.
c. Applies the master page.
2. Init: The page takes the following actions in the Init phase of its life cycle:
a. Recursively initializes the controls in its Controls collection. This initialization includes setting the properties of these controls such as Page, ID, NamingContainer, and so on.
b. Recursively applies these controls’ skins.
c. Calls its own OnInit method to raise its own Init event and then recursively calls the child control’s OnInit methods to raise their Init events.
d. Calls its own TrackViewState to start its own view state tracking and then recursively calls the child controls’ TrackViewState methods to start their view state tracking.
3. InitComplete: The page calls its OnInitComplete method to raise the InitComplete event. This event signals the end of the initialization phase. By this time all controls in the Controls collection of the page are initialized.
4. LoadControlState(postback only): The page recursively calls the LoadControlState method of those controls in its Controls collections that have called the RegisterRequiresControlState method of the page class to express interest in using their control states.
5. LoadViewState(postback only): The page first calls its own LoadViewState method and then recursively calls the LoadViewState method of the controls in its Controls collection to allow them to load their saved view states.
6.  LoadPostData(postback only – first try): The page calls the LoadPostData method of the controls that implement the IPostBackDataHandler interface and passes the posted data into it. The LoadPostData method of each control must access the posted data and update the respective property of the control accordingly. For example, the LoadPostData method of the TextBox control assigns the new value of the text box to the Text property of the TextBox control.
7. PreLoad: The page calls its OnPreLoad method to raise the PreLoad event. This event signals the beginning of the load phase of the page life cycle.
8. Load: The page calls its own OnLoad method to raise its own Load event and then recursively calls the OnLoad methods of the controls in its Controls collection to raise their Load events. Page developers may register callbacks for the Load event, where they may programmatically add child controls to the Controls collection of the page.
9. LoadPostData(postback only second try): The page calls the LoadPostData method of those controls that where programmatically added to its Controls collection in the Load phase if they implement the IPostBackDataHandler interface.
10. RaisePostDataChangedEvent(postback only): The page calls the RaisePostDataChangedEvent method of those controls whose LoadPostData method returned true. The RaisePostDataChangedEvent method raises post data changed event. For example, the TextBox control raises this event when the new value of the text box is different from the old value.
11. RaisePostbackEvent(postback only): The page calls the RaisePostBackEvent method of the control whose associated HTML element submitted the form. For example, the Button control’s associated HTML element posts the page back to the server. The RaisePostBackEvent method of a control must map the postback event to one or more server-side events. For example, the RaisePostBackEvent method of the Button control maps the postback event to the Command and Click server-side events.
12. LoadComplete: The page calls its OnLoadComplete method to raise the LoadComplete event to signal the completion of all loading activities including loading post data and raising post data changed event to allow interested controls to update themselves accordingly.
13. RaiseCallBackEvent(postback and callback only): The page calls the RaiseCallBackEvent method of the control that uses the ASP.NET client callback mechanism to allow client-side method(such as a JavaScript function) to call a server-side method  without having to post the entire page back to the server. The RaiseCallbackEvent method must call the respective server-side methods. If the page is posted back through the client callback mechanism, the page will not go through the rest of its lifecycle phases.
14. PreRender: The page takes the following actions in this phase of its life cycle:
a. Calls its EnsureChildControls method to ensure its child controls are created before the page enters its rendering phase.
b. Calls its own OnPreRender method to raise its own PreRender event.
c. Recursively calls the OnPreRender methods of the controls in its Controls collection to raise their PreRender events.
15. PreRender Complete: The page calls its OnPreRenderComplete method to raise the PreRenderComplete event to signal the completion of all prerendering activities.
16. SaveControlState: The page recursively calls the SaveControlState method of those controls in its Controls collection that have called the RegisterRequiresControlState method of the page class to express interest in saving their control states.
17. SaveViewState: The page first calls its own SaveViewState method and then calls the SaveViewState method of the controls in its Controls collection to allow them to save their view states.
18. SaveStateComplete: The page calls its OnSaveStateComplete method to raise the SaveStateComplete event to signal the completion of all save state activities.
19. Rendering: The page takes the following actions in this phase of its life cycle:
a. Creates an instance of the HtmlTextWriter class that encapsulates the output stream of the response.
b. Calls its RenderControl method and passes the HtmlTextWriter instance into it.
The RenderControl method recursively calls the Render Control methods of the child controls to allow each child control to render its HTML markup text. The HTML markup texts of the child controls form the final markup text that is sent to the client browser.

No comments:

Post a Comment

Popular posts