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. Understanding the page life cycle is important because doing so allows you to write code at the appropriate stage of the life cycle to achieve the desired results. In addition, if you are developing a custom control, you must familiarize yourself with the page life cycle to properly initialize the controls, populate the control properties with view-state data, and run any control behavior code. (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 is an application phase before and after the request, but these stages are not specific to the page.

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

View state is saved for the page and all controls before rendering. During the rendering phase, the page calls the Render method for each control, and it provides a text writer to write the output of the control to the outputstream of the Response property of the page.

Unloading

The unload is called when the page is fully rendered and the page has been sent to the client, and the page is 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 them 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 the property defaults to True), the page event is automatically bound to a method that uses the naming convention of the Page_ event, 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. In addition to the events listed, there are other events, but most page processing scenarios do not use these events. 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

PreInit

Use this event to perform the following actions:

  • Check the IsPostBack property to determine whether the page is being 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.

    Description: 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 event.

Init

Raised after all controls have been initialized and all skin settings have been applied. Use this event to read or initialize the properties of a control.

InitComplete

Raised by the Page object. Use this event to handle tasks that require that all initialization work be completed first.

Preload

Use this event if you need to perform processing on a page or control before the Load event.

After the Page raises the event, it loads the view state for itself and all the controls, and then processes any postback data that is included in the Request instance.

Load

The page increases the OnLoad event method on page and then recursively performs the same action on each child control, so that it repeats until the page and all controls are loaded.

Use the OnLoad event method to set the properties in the control and establish a database connection.

Control events

Use these events to handle specific control events, such as the Button control's Click event or the TextChanged event of a TextBox control.

Description: In a postback request, if the page contains a validator control, check the IsValid property of the page and individual validation controls before performing any processing.

LoadComplete

Use this event for a task that needs to load all other controls on the page.

PreRender

Before the event occurs:

  • The Page object calls EnsureChildControls for each control and page.

  • Each data-bound control that has the DataSourceID property set will call the DataBind method. For more information, see Data-binding events for data-bound controls later in this topic.

The PreRender event occurs for each control on the page. Use this event to make a final change to the contents of a page or its controls.

Savestatecomplete

ViewState has been saved for the page and all controls before the event occurred. Any changes made to the page or control at this time will be ignored.

Use this event to perform tasks that require that view state be saved, but that no changes are made to the control.

Render

This is not an event; At this stage of processing, the Page object will be called on each control with this method. All ASP. NET Web server controls have a Render method that is used to write the control tokens that are sent to the browser.

If you create a custom control, you typically override this method to output the markup for the control. However, if your custom control merges only standard ASP. NET Web server controls and does not merge custom tags, you do not need to overwrite the Render method. For more information, see Developing Custom ASP. NET server controls.

The user control (. ascx file) automatically merges the rendering, so you do not need to explicitly render the control in your code.

Unload

The event occurs first for each control, and then for that page. In a control, use this event to perform a final cleanup of a specific control, such as closing a control-specific database connection.

For the page itself, use this event to perform the final cleanup work, such as closing open files and database connections, or completing logging or other requests for specific tasks.

Description: 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.

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.