ASP. NET page Execution order (ASP. NET life cycle)

Source: Internet
Author: User

This section describes only part of the life cycle:

---referencing MSDN

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

At the beginning, page properties, such as request and response, will be set at this stage, and the page will also determine whether the request is a postback request or a new request, and set 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.

Program test page load Order

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls;  7 8 Namespace Order 9 {public partial class TestControls:System.Web.UI.Page11 {ten static int count = 0;13//414 protected void Page_Load (object sender, EventArgs e) {Response.Write ( Count + "----Page_Load <br/>"), count++;18}19//020 protected override void O Npreinit (EventArgs e) {base.         Onpreinit (e); Response.Write (count + "----onpreinit <br/>"); count++;25}26 127 protected override void OnInit (EventArgs e) (base. OnInit (e); Response.Write (count + "----OnInit <br/>") count++;32}33// 534 protected override void OnLoad (EventArgs e). OnloaD (e); PNs Response.Write (count + "----OnLoad <br/>"), count++;39}40//341 protected override void Onpreload (EventArgs e) for the base.         Onpreload (e); Response.Write (count + "----onpreload <br/>"); count++;46}47 648 protected override void Onloadcomplete (EventArgs e).         Onloadcomplete (e); Response.Write (count + "----onloadcomplete <br/>"); count++;53 }54//255 protected override void Oninitcomplete (EventArgs e).         Oninitcomplete (e); Response.Write (count + "----oninitcomplete <br/>"); count++;60 }61 protected override void OnUnload (EventArgs e). OnUnload (e);}65 protected override void OnDataBinding (EventArgs e). OndatabInding (e); Response.Write (count + "----ondatabinding <br/>"); count++;70}71 772 protected override void OnPreRender (EventArgs e).         OnPreRender (e); Response.Write (count + "----OnPreRender <br/>"); count++;77}78 protected void Btngraphics_click1 (object sender, EventArgs e) {Response.Write (count + ")-- --btngraphics_click <br/> "); 81}82}83}

Operation Result:

0----Onpreinit
1----OnInit
2----Oninitcomplete
3----Onpreload
4----Page_Load
5----OnLoad

----Button_Click

6----Onloadcomplete
7----OnPreRender

Corresponding Event Analysis:

onpreinit: The PreInit event is raised when the method is called. The main thing in this event is to apply the theme in App_Themes, and to apply the master page, usually this step we don't need to do.

OnInit: initializes the control to which the page is applied, and then applies the applied themes to the control, and the Init method of the recursive triggering subspace

Oninitcomplete: Its invocation indicates initialization is complete, all controls are initialized, all themes have been applied

onpreload: Its invocation indicates that the page enters the loading state, in which the view state (??) is loaded for itself and all controls. ), and then process the request instance containing all the data that will be sent

OnLoad: Commonly used events, that is, the Page_Load event, Page_xxx is a kind of time link form for all time, such as Page_Init will call the OnInit method. This event can handle postback, because the view state, Themes, the child controls are already initialized, so it is easy for us to add logic without worrying about being overwritten by other events. It is important to note that the load order of any page is the first of its own, and then the child controls, such as: An ASPX page is called by the Load event, loaded itself first, and then loaded such as master pages, user controls, third-party server controls, and so on

Onloadcomplete: indicates that the loading of all controls is complete. The next step is to render the loaded content

OnPreRender: This event handles some of the work that is prepared to render. For example, to DataBind a datasource control, we can change the value of some controls dynamically on this event.

Onprerendercomplete: This event marks the readiness to render complete, the data has been finished binding

saveviewstate: In this event, the ViewState is serialized, and then the attempt state is saved in the page as a Input:hidden element

Render: at this stage, the framework invokes the Render method of all controls to render the page. To produce an HTML page

Unload: Uninstall after the end of the page life cycle

ASP. NET page Execution order (ASP. NET 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.