An overview of the ASP page life cycle

Source: Internet
Author: User
Tags naming convention

When an ASP. NET page runs, this page goes through a life cycle, and a series of processing steps are performed during the life cycle. These steps include initializing, instantiating the control, restoring and maintaining the state, running the event handler code, and rendering. It is important to understand the life cycle of a page so that you can write code at the appropriate life cycle stage to achieve the desired results. In addition, if you develop a custom control, you must familiarize yourself with the page life cycle to properly initialize the control, populate the control properties with view-state data, and run all the control's behavior logic. (The life cycle of a control is based on the life cycle of the page, but the page raises more control events than are available in a separate ASP. ) General page life cycle stage

In general, pages go through each of the stages outlined in the following table. In addition to the page life cycle phase, there are application stages that occur before and after the request, but these stages are not specific to the page. For more information, see ASP. NET Application life cycle Overview .

 
Stage Description

Page request

Page requests occur before the page life cycle begins. When a user requests a page, ASP. NET determines whether the page needs to be parsed and compiled (thereby starting the life cycle of the page), or whether the cached version of the page can be sent in response without running the page.

Begin

In the start phase, page properties, such as Request and Response, are set. At this stage, the page also determines whether the request is a postback request or a new request, and sets the IsPostBack property. Also, during the start phase, the UICulture property of the page is set.

Page initialization

During page initialization, the controls in the page can be used, and the UniqueID property of each control is set. In addition, any theme will be applied to the page. If the current request is a postback request, the postback data has not yet been loaded, and the control property value has not been restored to the value in view state.

Load

During load, if the current request is a postback request, the control property will be loaded with information recovered from view state and control state.

Verify

During validation, the Validate method for all validator controls is invoked, and this method sets the IsValid properties for each validator control and page.

Postback event handling

If the request is a postback request, all event handlers are called.

Present

During rendering, view state is saved to the page, and then the page calls each control to provide its rendered output to the outputstreamof the page's Response property.

Unloading

The uninstallation is invoked when the page is fully rendered, when the page is sent to the client and ready to be discarded. At this point, the page properties (such as Response and Request) are unloaded and cleanup is performed.

Life Cycle Events

In each stage of the page life cycle, the page raises events that can run your own code for processing. For control events, you can bind an event handler to an event by declaratively using properties such as onclick, or by using code.

The page also supports automatic event connections, that is, ASP. NET will look for methods that have a specific name and automatically run these methods when a specific event is raised. If the AutoEventWireup property of the @ Page directive is set to true(or if the property is not defined because it is trueby default), the page event is automatically bound to use the Page_ Methods of the event naming convention, such as Page_Load and Page_Init. For more information about automatic event connections, see the ASP. NET Web server control event model .

The following table lists the most commonly used page life cycle events. The actual events are more than the listed events. However, they are not used for most page processing scenarios. Instead, they are primarily used by server controls on ASP. NET pages to initialize and render themselves. If you are writing your own ASP. NET server controls, you need to learn more about these phases. For information about creating custom controls, see Developing Custom ASP. NET server Controls .

 
Page Events Typical use

Page_PreInit

  • Use the IsPostBack property to determine whether the page is processed for the first time.

  • Creates or re-creates a dynamic control.

  • Dynamically set the master page.

  • Dynamically sets the Theme property.

  • Reads or sets the profile property value.

    Attention

    If the request is a postback request, the value of the control has not been restored from view state. If a control property is set at this stage, its value may be overwritten in the next phase.

Page_Init

  • Reads or initializes a control property.

Page_Load

  • Read and update control properties.

Control Events

To perform application-specific processing:

  • If the page contains validator controls, check the IsValid properties of the page and individual validation controls before performing any processing.

  • Handles a specific event, such as the Click event of a Button control.

Page_PreRender

  • Make the last change to the contents of the page.

Page_Unload

Perform the final cleanup work, which may include:

  • Close open files and database connections.

  • Complete log records or other request-specific tasks.

    Attention

    During the unload phase, the page and its controls are rendered so that no further changes can be made to the response stream. If you try to invoke a method, such as the Response.Write method, the page throws an exception.

Additional page life-cycle considerations

Note the following additional information about the page life cycle:

  • Each ASP. NET server control has its own life cycle, which is similar to the page life cycle. For example, the Init and Load methods of the control are called during the corresponding page event. If the page contains controls, the control's init method is called first, and then the page's init method is called. However, the load method of the page is called before the control's load method is called.

  • You can customize the appearance or content of a control by handling the control's events. For example, all controls will throw Init , Load , and Unload events, but page developers typically do not handle these events. Instead, you typically handle control-specific events, such as the click Event of the Button control and the listbox The control's selectedindexchanged event. In some cases, you may also want to handle the DataBinding or databound events of the control. For more information, see Class reference topics for individual controls and develop custom ASP. NET server Controls .

  • In addition to handling events raised by a page, you can override methods in the base class of the page. For example, you can override the page's initializeculture method to dynamically set culture information. Note that when you create an event handler using the Page_event syntax, the base implementation is called implicitly, so you do not need to call it in the method. For example, the OnLoad method of the page base class is always called regardless of whether the Page_Load method is created. However, if you use the override keyword ( Overridesin Visual Basic) to override the OnLoad method for a page, you must explicitly call the base method. For example, if you override the OnLoad method in a page, you must call base. Load( mybase.loadin Visual Basic) to run the base implementation.

An overview of the ASP page life cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.