Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of the life cycle of ASP.NET pages

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the "detailed explanation of the life cycle of the ASP.NET page". In the daily operation, I believe that many people have doubts about the detailed explanation of the life cycle of the ASP.NET page. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubts of "the detailed explanation of the life cycle of the ASP.NET page". Next, please follow the editor to study!

When the ASP.NET page runs, the page goes through a life cycle in which a series of processing steps are performed. This includes initialization, instantiation of controls, restoration and maintenance of state, runtime handler code, and rendering. It is important to be familiar with the page lifecycle so that we can write code at the appropriate stage of the lifecycle. It would be nice if we could write code thinking about what steps in the life cycle we are doing right now.

Several representative problems

At the beginning, let's think about a few questions and see if we can answer these questions when we finish describing the life cycle of the page.

1. Why can the data submitted by the user be obtained through this.textbox1.Text on the server side?

two。 In Page_Load, Response.Write ("hello") looks at the original file of the generated html code. Where is hello? Why?

3. There is a server-side button that sets the click event. When will the click event be executed? Do you want to execute the Page_Load event or the click event first?

4. Why can the client display the value after setting the value through this.textbox1.Text on the server side?

Understand the ASP.NET request pipeline, application life cycle, and overall operation mechanism. Children may know that in the ASP.NET application cycle, the relationship between PreRequestHandlerExecute events and PostRequestHandlerExecute events is our page life cycle. For aspx pages, it is a series of page control trees that trigger various page times. For general handlers, ashx is to directly implement the ProcessRequest method written by our developers. For MVC applications, it's all about creating controller factories, creating controller objects, and calling Action.

The following is mainly about the life cycle of pages in ASP.NET WebForm.

When we use the decompiler to view the ProcessRequest method of the Page class, we can see that when we first call FrameworkInitialize; FrameworkInitialize, we create a page control tree, and then we call ProcessRequestMain, which starts the execution of the entire page life cycle (in fact, a series of event methods are called) (maybe some of the pictures can't be seen on the right, but you can open the picture in the new tab).

1. Build a page control tree

The _ buildControlTree () method is called internally by FrameworkInitialize

In the image above, the code for the foreground page is on the left, and the code for creating the control tree is on the right. What is intercepted in the middle is the code that generates that part of the form.

Let's look at a schematic diagram.

The browser's DOM tree is based on the Html tag to generate a C language DOM tree, while the ASP.NET server side is a control tree built with C #, which is also built according to the DOM structure. It's the same in essence. Everything on the server side is added to the control collection of the page object. The tag uses the control object when there is a corresponding control object on the server side, and encapsulates it with LiteralControl when it is not available. Both server controls and string tags (no runat= "server" tags) are stored as control objects in the control collection of the foreground page class. The advantage is that when generating the html code of the foreground page, you only need to traverse the RenderControl method of each control object in the control collection, and each control will call its own Render method to generate the corresponding Html string. Then the generated html strings of all controls are restored to the html code of a page.

two。 Trigger PerformPreInit event

This event is initialized before all initialization, mainly initializing the theme and initializing the master page

Private void PerformPreInit () {this.OnPreInit (EventArgs.Empty); this.InitializeThemes (); this.ApplyMasterPage (); this._preInitWorkComplete = true;}

3. Trigger InitRecursive event

4. Trigger the LoadAllState () event

Load the page state parsing ViewState, de-Base64 the ViewState in the page form, deserialize, and store it in the ViewState attribute of the page.

5. Trigger ProcessPostData (this._requestValueCollection, true) event

Private void ProcessPostData (NameValueCollection postData, bool fBeforeLoad) {if (this._changedPostDataConsumers = = null) {this._changedPostDataConsumers = new ArrayList ();} if (postData! = null) {foreach (string strin postData) {if ((str! = null) & &! IsSystemPostField (str)) {Control control = this.FindControl (str); if (control = = null) {if (fBeforeLoad) {if (this._leftoverPostData = null) {this._leftoverPostData = new NameValueCollection () } this._leftoverPostData.Add (str, null);}} else {IPostBackDataHandler postBackDataHandler = control.PostBackDataHandler; if (postBackDataHandler = = null) {if (control.PostBackEventHandler! = null) {this.RegisterRequiresRaiseEvent (control.PostBackEventHandler);}} else {if (postBackDataHandler! = null) {NameValueCollection postCollection = control.CalculateEffectiveValidateRequest ()? This._requestValueCollection: this._unvalidatedRequestValueCollection; if (postBackDataHandler.LoadPostData (str, postCollection)) {this._changedPostDataConsumers.Add (control);}} if (this._controlsRequiringPostBack! = null) {this._controlsRequiringPostBack.Remove (str);}} ArrayList list = null; if (this._controlsRequiringPostBack! = null) {foreach (string str2 in this._controlsRequiringPostBack) {Control control2 = this.FindControl (str2) If (control2! = null) {IPostBackDataHandler adapterInternal = control2.AdapterInternal as IPostBackDataHandler; if (adapterInternal = = null) {adapterInternal = control2 as IPostBackDataHandler;} if (adapterInternal = = null) {object [] args = new object [] {str2}; throw new HttpException (SR.GetString ("Postback_ctrl_not_found", args));} NameValueCollection values2 = control2.CalculateEffectiveValidateRequest ()? This._requestValueCollection: this._unvalidatedRequestValueCollection; if (adapterInternal.LoadPostData (str2, values2)) {this._changedPostDataConsumers.Add (control2);}} else if (fBeforeLoad) {if (list = = null) {list = new ArrayList ();} list.Add (str2);}} this._controlsRequiringPostBack = list;}}

Mainly did two things.

1) set the control data submitted in the form to the properties of the corresponding control in the control tree of the page object (give a value to the control in the control tree created earlier), so that you can get the value entered by the client on the server side.

2) compare the submitted value in the form with the original value of the control in ViewState. If the difference means to trigger the Change event of the control, then put the control into a collection at the same time (see that the source code is actually changedPostDataConsumers). During subsequent execution, traversing the collection triggers the Change event of the corresponding control in turn.

6. Trigger the LoadRecursive () event

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report