Understanding the life cycle of WebForm in ASP-Practical tips

Source: Internet
Author: User
This article mainly introduces to you about the WebForm life cycle of the relevant data, the introduction of the very galaxy, for everyone has a certain reference learning value, the need for friends to follow the small series together to learn it.

Objective

This article mainly introduces to you about the WebForm life cycle of the relevant content, sharing out for everyone to refer to the study, the following to see the detailed introduction:

A The concept of the ASP. NET page life cycle

When we enter the URL in the browser address bar, enter to view the page, this will send a request to server-side IIS, the server will determine the requested page sent over, when fully recognize the TTP page handler class, ASP. The NET runtime invokes the ProcessRequest method of the handler to process the request, creating the Page object. Typically, you do not 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: Initialize, load view state information and postback data, load user code for the page, and perform postback server-side events. After that, the page enters display mode: Collects the updated view state, generates the HTML code, and then sends the code to the output console. Finally, the page is uninstalled and the request is processed. The process of handling this series of events where the Page object ProcessRequest method completes is the ASP. NET page life cycle.

Ii. Why you need to understand the ASP. NET page life cycle

Because understanding the ASP. NET page life cycle, you can help developers write programs at the right stage of their life cycle to achieve the desired results, and if you want to develop custom controls yourself, you must familiarize yourself with the life cycle of the page to properly initialize the controls, populate the control's properties with view state data, and run any control behavior code. That is to say only familiar with from create to finally unload a series of events, development will be comfortable, will not appear foggy feeling.

Iii. phase of life cycle

1. Request page: Page requests occur before the page life cycle begins.

2. Start: In the Beginning phase, 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.

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

4. Load page: 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.

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

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

7. Render page: During page rendering, view state will be saved to the page, and then the page will call each control to provide its rendered output to the outputstream of the page's response property.

8. Uninstall page: When the page is fully rendered, the page is sent to the client and ready to be discarded, the uninstallation is invoked. At this point, the page properties (such as response and request) are unloaded and cleanup is performed.

Iv. events in the life cycle

1, 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.

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

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

3, InitComplete

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

4, 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.

5. 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 properties in a control and establish a database connection

6. 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.

Attention:

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.

7, LoadComplete

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

8, PreRender

Before the event occurs:

The Page object is 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 below.

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.

9, 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.

10. 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 override 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.

11, 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.

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.

Summarize

Related Article

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.