Section 6.5. Lifecycle


6.5. Lifecycle

A user sits at her browser and types in a URL. A web page appears, with text and images and buttons and so forth. She fills in a text box and clicks a button. What is going on behind the scenes?

Every request made of the web server initiates a sequence of steps. These steps, from beginning to end, constitute the lifecycle of the page.

When a page is requested from the server, it is loaded into server memory, processed , sent to the user, and unloaded from memory. From one end of the lifecycle to the other, the goal of the page is to render appropriate HTML to the requesting browser. At each step, methods and events are available to let you override the default behavior or add your own programmatic enhancements.

To understand the lifecycle of the page and its controls, you must recognize that the Page class creates a hierarchical tree of all the controls on the page. All the components on the page, except for directives, are part of this control tree . You can see the control tree for any page by adding trace="true" to the Page directive. (Directives are described in the next section of this chapter. Chapter 7 discusses tracing in detail.)

The Page itself is at the root of the tree . All the named controls are included in the tree, referenced by control ID . Static text, including whitespace , newlines , and HTML tags, are represented in the tree as LiteralControls. The order of controls in the tree is strictly hierarchical. Within a given level of the hierarchy, the controls are in the order in which they appear in the content file.

Web components, including the Page , go through the entire lifecycle every time the page is loaded . (This involves a fair amount of performance overhead, which you can reduce somewhat by caching. Caching and performance are covered in Chapter 17.) Events fire first on the Page , then recursively on every object in the control tree.

The following is a detailed description of each of the phases of the component lifecycle in a web form. There are two slightly different sequences of events in the lifecycle: on the first loading of the page and on subsequent postbacks. This lifecycle is shown schematically in Figure 6-7.

Figure 6-7. Web page lifecycle

During the first page load, the lifecycle is composed of the following steps:

  1. The constructor is run. This is the first step in the lifecycle for the Page or any control on the page.

  2. Determine the PostBack mode. Is this the first load, a self-postback, or a cross-page post?

  3. Pre-initialization. This step is necessary to determine the target device before the page is initialized . PreInit is the first event in the lifecycle that can be trapped and handled, using the OnPreInit method.

  4. Personalization and themes are loaded and initialized.

  5. Initialization. The initialization phase is when the control tree is built . In this phase, you can initialize any values needed for the duration of the request.

    The initialize phase is modified by handling the Init event with the OnInit method.

  6. Pre-load. Occurs before postback data is loaded in the controls. This phase can be modified by handling the PreLoad event with OnPreLoad method.

  7. Load. User code runs and the form controls show client-side data. View state information is available and controls in the Page 's control hierarchy can be accessed.

    The load phase can be modified by handling the Load event with the OnLoad method.

  8. Load complete. The load process is completed. The LoadComplete event can be handled by the OnLoadComplete method.

  9. Pre-render. This is the phase just before the output is rendered. Modifications are made via the PreRender event, using the OnPreRender method.

  10. Save state. Personalization, Control, View and Page state information is saved.

  11. Render. The page and its controls are rendered as HTML . You can override using the Render method . Within Render , CreateChildControls is called, if necessary, to create and initialize server controls in the control tree.

  12. Unload. This is the last phase of the lifecycle . It gives you an opportunity to do any final cleanup and release references to any expensive resources, such as database connections . This is important for scalability. It can be modified using the OnUnload method.

During postback, the lifecycle is the same as during the first load, except for the following:

  1. Load State. After initialization is complete, the Page and Control state is loaded and the ViewState is loaded from a hidden variable on the page as described in "View State" earlier in this chapter.

  2. Postback data is processed. During this phase, the data sent to the server via the Post method is processed. Any updates to the view state necessitated by the postback are performed via the LoadPostData method.

  3. Pre-load and load. Same as on first load.

  4. Send postback change notifications. If any state changes between the current state and the previous state occur, change events are raised via the RaiseChangedEvents method . The events are raised for the controls in the order in which the controls appear in the control tree.

  5. Handle postback events. Exactly one user action caused the postback . That user action is handled now, after all the change events have been handled . The original client-side event that instigated the postback is handled in the RaisePostBackEvent method.

  6. Load complete. From here on out, it is the same as on first load.



Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 173

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net