ASP. NET Page Lifecycle Overview of Summary

  • 2021-10-27 07:08:17
  • OfStack

When the ASP. NET page is run, the page will go through a life cycle in which a series of processing steps will be performed. These steps include initializing, instantiating controls, restoring and maintaining state, running event handler code, and rendering. It is important to understand the life cycle of a page so that you can write code at the right stage of the life cycle to achieve the desired results. In addition, if you develop custom controls, you must be familiar with the page lifecycle to properly initialize controls, populate control properties with view state data, and run all control behavior logic. (The lifecycle of the control is based on the lifecycle of the page, but the page raises more control events than are available in a separate ASP. NET page.)

General Page Lifecycle Stages

1 Generally speaking, pages go through various stages outlined in the following table. In addition to the page lifecycle phases, there are application phases that occur before and after the request, but these phases are not page-specific. For more information, see ASP. NET Application Lifecycle Overview.

Stage description

Page request

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

Begin

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

Page initialization

During page initialization, the controls in the page are available and the UniqueID property for 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 been loaded and the control property values have not been restored to values in view state.

Loading

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

Validation

During validation, the Validate method of all validator controls is called, which sets the IsValid properties of individual validator controls and pages.

Postback event handling

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

Render

During rendering, the view state is saved to the page, which then calls each control to provide its rendered output to the OutputStream of the Response property of the page.

Uninstall

When the page is rendered completely, sent to the client and ready to be discarded, the uninstall is called. At this point, page properties such as Response and Request are unloaded and cleanup is performed.

Life cycle events

At each stage of the page life cycle, the page raises events that you can run your own code to handle. For control events, you can bind event handlers to events either by declaratively using properties such as code.

Pages also support automatic event joins, that is, ASP. NET looks for methods with specific names and automatically runs them when specific events are raised. If the AutoEventWireup property of the @ Page directive is set to true (or if this property is not defined because it is true by default), page events are automatically bound to methods that use the Page_event naming convention, such as Page_Load and Page_Init. For more information about automatic event connections, see ASP. NET Web Server Control Event Model.

The following table lists the most commonly used page life cycle events. There are more actual events than listed. However, they are not used in most page processing scenarios. Instead, it is mainly used by server controls on ASP. NET Web pages to initialize and render themselves. If you want to write your own ASP. NET server control, you need to know these phases in detail. For information about creating custom controls, see Developing Custom ASP. NET Server Controls.

Page events typically use the

Page_PreInit

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

Create or recreate a dynamic control.

Dynamic setting master page.

Dynamically set the Theme property.

Reads or sets profile property values.

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

Page_Init

Reads or initializes control properties.

Page_Load

Read and update control properties.

Control events

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 specific events, such as the Click event of an Button control.

Page_PreRender

Make final changes to the contents of the page.

Page_Unload

Perform the final cleanup, which may include:

Close open file and database connections.

Complete logging or other request-specific tasks.

Note that during the uninstall phase, the page and its controls have been rendered, so you cannot make further changes to the response flow. If you try to call a method, such as the Response. Write method, the page throws an exception.

Additional Page Lifecycle 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 control's Init and Load methods are called during the corresponding page event. If the page contains controls, the Init method of the control is called first, and then the Init method of the page is called. However, the page's Load method is called before the control's Load method is called.

You can customize the appearance or content of a control by handling its events. For example, all controls raise Init, Load, and Unload events, but page developers typically do not handle these events. Instead, it typically handles control-specific events, such as the Click event for the Button control and the SelectedIndexChanged event for the ListBox control. In some cases, you may also need to handle DataBinding or DataBound events for the control. For more information, see Class Reference Topics for Individual Controls and Developing Custom ASP. NET Server Controls.

In addition to handling events raised by the page, you can override methods in the base class of the page. For example, you can override the InitializeCulture method of a page to dynamically set culture information. Note that when an event handler is created using the Page_event syntax, the base implementation is implicitly invoked, so it does not need to be called in a method. For example, the method of the page base class is always called regardless of whether the Page_Load method is created or not. However, if you override a page's method using the override keyword (Overrides in Visual Basic), you must explicitly call the base method. For example, if you override a method in a page, you must call. Load (My. Load in Visual Basic) to run the base implementation.


Related articles: