ASP.NET页面生命周期

摘自:《Professional ASP.NET 2.0 Server Control and Component Development》p126


The page goes through the following phases (notice some phases are marked as postback only because
these are the phases that the page goes through only when it is posted back to the server):

1.    The page first retrieves the posted data from the QueryStringor Formcollection of the
Requestobject.

2.    The page then checks whether the posted Datacollection (the NameValueCollectionFormor
QueryString) contains an item with the key __CALLBACKID. If it does, it sets its IsCallback
Boolean property to trueto signal that the page has been posted back to the server through the
ASP.NET client callback mechanism. This mechanism is thoroughly discussed in Chapter 26.

3.    PreInit: The page takes the following actions in the PreInitphase of its life cycle:
    a. Calls its OnPreInitmethod to raise the PreInitevent.
    b. Initializes the theme by using the contents of the App_Themesdirectory to dynamically
        implement a class of type PageTheme, compiles the class, creates an instance of the
        compiled class, and assigns the instance to its PageThemeproperty.
    c.  Applies the master page.

4.    Init: The page takes the following actions in the Initphase of its life cycle:
    a. Recursively initializes the controls in its Controlscollection. 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 OnInitmethod to raise its own Initevent and then recursively calls the
        child control’s OnInitmethods to raise their Initevents.
    d. Calls its own TrackViewStateto start its own view state tracking and then recursively
        calls the child controls’ TrackViewStatemethods to start their view state tracking.
5.    InitComplete: The page calls its OnInitCompletemethod to raise the InitCompleteevent.
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 LoadControlStatemethod
of those controls in its Controlscollection 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 LoadViewStatemethod and then
recursively calls the LoadViewStatemethod of the controls in its Controlscollection to allow
them to load their saved view states.

8.    Load Post Data (postback only - first try): The page calls the LoadPostDatamethod of the con-
trols that implement the IPostBackDataHandlerinterface and passes the posted data into it.
The LoadPostDatamethod of each control must access the posted data and update the respec-
tive property of the control accordingly. For example, the LoadPostDatamethod of the TextBox
control assigns the new value of the text box to the Textproperty of the TextBoxcontrol.

9.    PreLoad: The page calls its OnPreLoadmethod to raise the PreLoadevent. This event signals
the beginning of the load phase of the page life cycle.

10.    Load: The page first calls its own OnLoadmethod to raise its own Loadevent and then recur-
sively calls the OnLoadmethods of the controls in its Controlscollection to raise their Load
events. Page developers may register callbacks for the Loadevent, where they may program-
matically add child controls to the Controlscollection of the page.

11.    Load Post Data (postback only second try): The page calls the LoadPostDatamethod of those
controls that were programmatically added to its Controlscollection in the Loadphase if they
implement the IPostBackDataHandlerinterface.

12.    Raise Post Data Changed Event (postback only): The page calls the RaisePostData
ChangedEventmethod of those controls whose LoadPostDatamethod returned true. The
RaisePostDataChangedEventmethod raises post data changed event. For example, the
TextBoxcontrol 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 RaisePostBackEventmethod of the
control whose associated HTML element submitted the form. For example, the Buttoncontrol’s
associated HTML element posts the page back to the server. The RaisePostBackEventmethod
of a control must map the postback event to one or more server-side events. For example, the
RaisePostBackEventmethod of the Buttoncontrol maps the postback event to the Command
and Clickserver-side events.

14.    Load Complete: The page calls its OnLoadCompletemethod to raise the LoadCompleteevent
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 RaiseCallbackEventmethod 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. The ASP.NET client callback mechanism is
discussed thoroughly in Chapter 26.

16.    PreRender: The page takes the following actions in this phase of its life cycle:
    a. Calls its EnsureChildControlsmethod to ensure its child controls are created before
        the page enters its rendering phase.
    b. Calls its own OnPreRendermethod to raise its own PreRenderevent.
    c.  Recursively calls the OnPreRendermethods of the controls in its Controlscollection
        to raise their PreRenderevents.

17.    PreRender Complete: The page calls its OnPreRenderCompletemethod to raise the
PreRenderCompleteevent to signal the completion of all prerendering activities.

18.    Save Control State: The page recursively calls the SaveControlStatemethod of those controls
in its Controlscollection that have called the RegisterRequiresControlStatemethod of
the page class to express interest in saving their control states.

19.    Save View State: The page first calls its own SaveViewStatemethod and then calls the
SaveViewStatemethod of the controls in its Controlscollection to allow them to save
their view states.

20.    Save State Complete: The page calls its OnSaveStateCompletemethod to raise the
SaveStateCompleteevent 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 HtmlTextWriterclass that encapsulates the output stream
        of the response.
    b. Calls its RenderControlmethod and passes the HtmlTextWriterinstance into it.
        The RenderControlmethod recursively calls the RenderControlmethods of the child con-
        trols 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.

生命周期图:


posted @ 2008-02-19 16:50  vento  阅读(740)  评论(0编辑  收藏  举报