Page Life Cycle

       As a control developer,you must gain a solid understanding of the page life cycle and its phases,which are shown as below:(copy from Professional ASP.NET 2.0 Server Control and Component Development)

  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. SameViewState
  18. SaveStateComplete
  19. Render 

1.

The page first retrieves the posted data from the QueryString or Form collection of the
Request object.
2.

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 client callback mechanism.

3.

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.
4.

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.
5.

 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.
6.

 Load Control State (postback only): The page recursively calls the LoadControlState method
of those controls in its Controls collection that have called the RegisterRequiresControlState
method of the page class to express interest in using their control states.
7.

 Load View State (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.
8.

 Load Post Data (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.
9.

 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.

10.

Load: The page first 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.
11.

Load Post Data (postback only second try): The page calls the LoadPostData method of those
controls that were programmatically added to its Controls collection in the Load phase if they
implement the IPostBackDataHandler interface.
12.

Raise Post Data Changed Event (postback only): The page calls the RaisePostData
ChangedEvent 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.
13.

Raise Postback Event (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.
14.

 Load Complete: 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.
15.

Raise Callback Event (postback and callback only): The page calls the RaiseCallbackEvent
method of the control that uses the ASP.NET client callback mechanism to allow a 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 life cycle phases.

16.

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.
17.

PreRender Complete: The page calls its OnPreRenderComplete method to raise the
PreRenderComplete event to signal the completion of all prerendering activities.
18.

Save Control State: 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.
19.

Save View State: 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.

20.

Save State Complete: The page calls its OnSaveStateComplete method to raise the
SaveStateComplete event to signal the completion of all save state activities.
21.

 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 RenderControl methods of the child controls
to allow each child control to render its HTML markup text. The HTML markup texts of
child controls form the final HTML markup text that is sent to the client browser.

 

      So that's the procedure of page's generation, perhaps someone don't known the difference between Postback and Callback, later,I'll write a article to explain it.

posted on 2009-02-02 12:44  相信你,相信我  阅读(304)  评论(0)    收藏  举报

导航