In depth understanding of the life cycle of WebForm in Asp. Net

  • 2021-10-11 18:05:39
  • OfStack

Preface

This article mainly introduces the relevant contents of WebForm life cycle in Asp. Net, and shares them for your reference and study. Let's take a look at the detailed introduction below:

1. Asp. Net Page Lifecycle Concept

When we enter the URL in the browser address bar and enter to view the page, we will send an request request to the server IIS, and the server will judge the sent request page. When the TTP page handler class is fully recognized, ASP. NET runtime will call the ProcessRequest method of the handler to process the request and create a page object. Typically, there is no need to change the implementation of this method because it is provided by the Page class. Next, the ProcessRequest method of creating the page object makes the page go through various stages: initialization, loading view state information and postback data, loading user code for the page, and executing postback server-side events. After that, the page goes into display mode: the updated view state is collected, the HTML code is generated, and then the code is sent to the output console. Finally, uninstall the page and consider the request processed. Asp. Net page lifecycle is the processing process of this series of events completed by the page object ProcessRequest method.

2. Why do you need to understand the Asp. Net page lifecycle

Because you understand the Asp. Net page lifecycle, It can help developers write programs at the appropriate stage of the life cycle to achieve the desired results. In addition, if you want to develop custom controls yourself, you must be familiar with the life cycle of the page in order to initialize the controls correctly, populate the properties of the controls with view state data, and run any control behavior code. That is to say, only familiar with 1 series of events from creation to final uninstallation will be developed smoothly, and there will be no feeling in the clouds.

3. Lifecycle phase

1. Request a page: Page requests occur before the start of the page life cycle.

2. Start: 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.

3. Initialize the page: During page initialization, the controls in the page can be used, and the UniqueID property of each control will be set. 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.

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

5. Validation: During validation, the Validate method of all validator controls is called, which sets the IsValid property of each validator control and page.

6. Postback event handling: If the request is a postback request, all event handlers will be called.

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

8. Uninstall the page: Render the page completely, and uninstall will be called when the page is sent to the client and ready to be discarded. At this point, page properties such as Response and Request are unloaded and cleanup is performed.

4. Events in the life cycle

1. PreInit

Use this event to do the following:

Check the IsPostBack property to determine if the page is being processed for the first time.

Create or recreate a dynamic control.

Dynamic setting master page.

Dynamically set Theme properties.

Reads or sets profile property values.

Note:

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 overridden in the next 1 event.

2. Init

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

3. InitComplete

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

4. PreLoad

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

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

5. Load

Page calls the OnLoad event method on Page, and then recursively does the same for each child control, and so on, until the page and all controls are loaded.

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

6. Control Events

Use these events to handle specific control events, such as the Click event for an Button control or the TextChanged event for an TextBox control.

Note:

In a postback request, if the page contains validator controls, check the Page and the IsValid properties of the individual validation controls before performing any processing.

7. LoadComplete

Use this event for tasks that need to load all other controls on the page.

8. PreRender

Before the event:

The Page object is EnsureChildControls for each control and page.

Each data-bound control with the DataSourceID property set calls the DataBind method. For more information, see Data Binding Events for Data Binding Controls below.

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

9. SaveStateComplete

ViewState was saved for the page and all controls before this event occurred. Any changes made to the page or control at this time are ignored.

Use this event to perform tasks that require that view state has been saved but that no changes have been made to the control.

10. Render

This is not an event; At this stage of processing, the Page object calls this method on each control. All ASP. NET Web server controls have an Render method that writes the token of the control sent to the browser.

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

The user control (. ascx file) is automatically merged and rendered, so it does not need to be explicitly rendered in code.

11. Unload

This event occurs first for each control and then for the 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 final cleanup, such as closing open files and database connections, or completing logging or other request-specific tasks.

Attention

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.

Summarize


Related articles: