ASP.NET page lifecycle

zhaozj2021-02-16  54

ASP.NET page lifecycle

Author: Solomon Shaffer translation: Lvshao Wei

Introduction

When a page request is sent to the web server, whether the event is excited by the page submit or by the page redirection, the page is running a series of events during its creation to the release. When we create an ASP.NET page, there is often no attention to its execution cycle, so that we encounter a lot of problems that make your headache. However, if you are properly used and manipulated, the page life cycle will become an efficient and powerful tool. Many developers realize that understanding what happens during the execution of the page and when it happens to efficiently write ASP.NET pages and user controls. So let's learn more about the ten events from the ASP.NET page from being created to its release.   擦 私 庠跹   庑 ┦   擞 小 小 小 小 小 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 提 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简 简This page will first load some server-side web controls. When the web server receives its request, the web server handles our web controls and eventually let us get the HTML presence of this page. The first step of the processing page is to initialize the object. Download Source Demo

1. Initialization

The controls in the page (including the page itself) are initialized in their initial form. Declare your object in the constructor of the ASPX page, the page will know the type of object and know how many such objects need to be created. Once you declare your controls in the constructor, you can access them in any of its subclasses, methods, events, or properties. However, if your object is the control specified in the ASPX file, such controls are not attribute. And doing this to access them from the code is dangerous, because these control instances cannot be created in order (assuming they can be completely created). Initialization events can be overloaded through the OnInit method.

Figure 1 control is initialized based on the statement

2. Load view status data

After initialization, the control can only be referenced by ID (no document object model for relatively reference). In the LoadViewState event, the initialized control has obtained the first attribute: the last submission of the view status information stored in the server. The page view status is maintained by ASP.NET, which is used to deposit information to the server in a round-trip. The view status information is saved as a name / value pair, which contains information such as Text and Value. The view information is saved in the page request in the page request in the value properties of the control. As you know, this is a huge leap in the old ASP3.0 status maintenance technology. This event can be overloaded through the LoadViewState method, often used to customize the data it accepts when the control is filled. Figure 2 shows an overload example of setting a view state in the LoadViewState event.

Figure 2 LoadViewState is excited, the appropriate view data is filled into the control.

3.LoadPostData Processing Backup Data

In the stage of creating a page, the FORM data sent to the server (the term in ASP.NET is the return data) in accordance with the data requirements of each control. When the page is submitted, the framework implements the iPostBackDataHandler interface on each of each submitted data. The page is then thrown the loadPostData event, and the control of the iPostBackDataHandler interface is discovered by page parsing, and the control status is updated with the correct return data. ASP.NET updates the correct control by matching the unique indicator of the control, which has the name value pair in the name of the name. This is also one of the reasons why each control is required in all specific pages. Other steps are done by the frame to determine that each marker is unique in the environment, such as a custom user control in a single page. After the LoadPostData event is activated, the RaisePostDataChanged event can be performed at any time (please continue to see)

4. Object load object Get the correct form in the LOAD event. All objects are first organized in the page DOM (called control tree in ASP.NET), and it is easy to reference through code or relative position (Crawling the Dom). The object can then be free access to client attribute sets in HTML, such as Width, Value, or Visibility. When loading, the control logic, such as an algorithm, set the control attribute in programming, and use the StringBuilder assembly output string to be executed at the same time. Most of the work is done at this stage. The LOAD event is overloaded by calling OnLoad, as shown in Figure 3.

Figure 3 Online event is the ideal location for placement logic

5. Stimulate the raisePostDataChanged event

As mentioned earlier, this happens after all controls that implements the IPostBackDataHandler interface are updated. In this process, each control has a boolean identifier that identifies its data of the control since the last submission is changed or retained. Then ASP.NET looks for any display control data to be changed by searching and exciting RaisePostDataChanged. After the RaisePostDataChanged event until the LOAD event occurs, all controls are updated after being updated. This guarantees that the data of other controls is not manually changed in the RaisePostDataChanged event before the control is returned data update.

6. Handle the client return event

When the reciprocating update causes a data change to raise the server-side event, the object that initiates the backhaul will be processed in the RaisePostBackeVent event. This excitation back-off object is often a state change to initiate the recovery control (its autopostback is enabled) or a clicked form submit button. Many of the code are executed in this event because this is the ideal location for controlling event-driven logic. In order to ensure the correctness of the data presented to the browser, the RaisePostBackeVent event is finally excited after a series of return events. Based on consistency, the changed control is changed until this function is executed. That is, the data that is expected to change is always reflected in the result page. RaisePostBackeVent events can be captured through raisePostBacke, as shown in Figure 4

Figure 4 RaisePostDataChanged and RaisePostBackeVent definitions by the iPostBackDataHandler interface

7. Object preview

Objects pre-presented are places where those that can be saved to a view or maintaining its view status. This makes pre-presentation steps to be the ideal location for final modifications, such as changing control properties or changing control tree structures, do not have to worry about changes in objects due to database requests or view status updates. After the pre-predend phase, the object change is locked and cannot be saved to the page view state. The pre-presentation phase can be achieved by overprenderender.

8. Save view status

The status of the view status is saved only after all the page object changes. Object status data is saved in hidden objects, which is also places where object status data is presented to HTML. In the SaveViewState event, the value can be saved to the view status object, but the change in the page control cannot be saved. This step can be implemented by overloading SaveViewState, as shown in Figure 5:

Figure 5 is set to the control in the onprender. In SaveViewState, the value is set to the view status object.

9. Rendering HTML

The RENDER event is created by assembling HTML for the browser output. In the Render event, the page call objects make them HTML, then the page collects HTML to send. When the Render event is overloaded, developers can create custom HTMLs for the browser, and any HTML created at this time has not taken effect. The render method uses the HTMLTextWriter object as a parameter and generates HTML to the browser. This will still be modified, but such modifications will only be reflected in the customer (the translator Note: It means that the change will only be reflected in the HTML presence and the view status cannot be changed). The RENDER event can be overloaded, as shown in Figure 6 (below) 10. Release

Once the page's HTML is rendered, the object is released. In the Dispose event, you can clear any objects or references constructed in the page creation. Here, all processing has been executed, you can safely release any other object, including the Page object. Dispose can be overloaded, as shown in Figure 6.

Figure 6 Render event generates custom HTML via HTMLTextWriter objects to the browser

to sum up

This time we ask an ASP.NET page, this process from initialization to the release is the same. By understanding the internal execution of the ASP.NET page, it will be easier and efficient when writing and debugging the code (more don't mention less discouraged.

转载请注明原文地址:https://www.9cbs.com/read-25597.html

New Post(0)