Introduction
ASP.NET is an integral part of Microsoft .NET strategy. It has great developments relative to the previous ASP, introduced many new mechanisms. This article makes a preliminary introduction to everyone on the life cycle of the ASP.NET page, in order to guide everyone to better, more flexible manipulating the role of ASP.NET. When a request to obtain a web page (may be completed by the user, it may be completed by hyperlink), which is then running from the created to the processing completion. When we try to establish an ASP.NET page, this execution cycle does not have to consider, so it will only be eaten. However, if the correct manipulation is properly manipulated, the execution cycle of a page will be a valid and powerful tool. Many developers discover when writing ASP.NETs and user controls, if you know what happened throughout the process, when you happen, you will have an important helpful role in completing the entire task. Let me introduce you to the Ten events from the ASP.NET page from the creation to the processing completion. At the same time, you also show you how to add your own code in these events to achieve a predetermined effect.
One. Initialization object
A page control (and page itself) should initialize correctly. By having all objects (as shown in Figure 1) in your C # file (Figure 1), the page knows how many objects to create and their types. Once you have all objects in your constructor, you can access them through inheritance, methods, events, or attributes. However, if your object is some of the controls specified in the ASPX file, then these controls do not have the properties. At the same time, access to them will generate some unexpected errors because these control instances are not a determined creation order (if they are created together). Also, you can overload the initialization event with OnIn, which is shown below (Figure 1):
figure 1
two. Import viewState data
After initialization, all controls can only be accessed by their IDs (because there is no corresponding DOM available). In the LoadViewState event, all controls will get their first properties: ViewState properties. This property will eventually return to the server to determine that this page is already accessed by the user or is still accessed by the user. The ViewState property is saved in the string of "Name / Value" pair, which contains information about the text and values of the control. This property is stored in the value properties of a hidden control, is passed when the request page is requested. This approach has great progress than the maintenance of ASP3.0, and it is determined that the page state has a great progress. Also, you can overreload the LoadViewState event function to set the value value for the corresponding control. The following figure (Figure 2) is an example:
figure 2
three. Handle Postback data with loadpostdata
This phase created at the page, the server submits the form data submitted on the page (called Postback data in the ASP.NET). When a page submits a form, the framework performs an iPostBackDataHandler interface operation on each control that submits the data. Then the page performs the loadPostData event, resolves the page, finds each control that performs the IPostBackDataHandler interface operation, and updates these control status with the appropriate Postback data. ASP.NET implements this by matching the "Name / Value" in the NameValue set to the unique ID match of each control. So, each control must have a unique ID on the page on the page of ASP.NET, and there can be a case where there are several controls. Even user-defined controls, the framework will also give them their own unique IDs. After the LoadPostData event, the following RaisePostDataChanged event is required. four. Import object
In the LOAD event, the object is instantiated. All objects are first arranged in the DOM page (in the ASP.NET "control tree) and can be referenced by code or related locations. Thus, the object can be easily obtained from the client, such as the attribute value, such as width, height, value, visibility, etc. in HTML, etc. In the LOAD event, of course, there is still an operation like setting control attributes. This process is the most important and main thing in the entire life cycle. You can overload the Load event by calling onLoad, as follows (Figure 3):
image 3
Fives. RaisePostBackChanged event
As mentioned above, this event is happening in all controls to perform the IPostBackDataHandler interface operation and is updated by the correct Postback data. In this process, each control is given a boolean value to mark that the control is updated. The ASP.NET then looks for any updated control and executes the RaisePostDataChanged event operation. However, this event is to be updated in all controls and after the LOAD event is completed. This ensures that one control is not manually changed in the RaisePostDataChanged event before being updated by Postback data.
six. Processing client postback events
When the event caused by the postback data is completed, the object of generating the Postback data is executed to execute the RaisePostBackeVent event operation. However, there will be this situation, because a change in a control state makes it return to the server or the user clicks the submit button to return to the server. In this case, there should be corresponding processing code to reflect the event-driven programming principle. Since it is necessary to meet the accuracy requirements of the data presenting to the browser, the RaisePostBackeVent event is the last occurrence. The controls changed during the Postback process should not be updated after the execution function function is called. That is, any data changed due to a expected event should be reflected on the final page. You can meet your requirements by modifying the RaisePostBackevent function, illustrated as follows (Figure 4):
Figure 4
Seven. Pre-present
You can change the object and change the last moment of saving is this step - pre-presented object. In this way, you can make final modifications on this step attribute, control tree structure, etc. It doesn't have to consider ASP.NET to make any changes, because the database calls have been discounted and the ViewState updated. After this step, all modifications to the object will eventually be determined, and it cannot be saved to the viewstate of the page. You can overload this step through OnPrender.
Eight. Save ViewState All ViewState is saved after the modification of the page control is complete. The status data of the object is retained inside the hidden control, and the status data presenting to HTML is also obtained here. In the SaveViewState event, its values can be saved to the ViewState object, but the changes in the control on the page cannot be changed. You can overload this step with SaveViewState, which is shown below (Figure 5):
Figure 5
nine. Hand to HTML
The render event occurs when using HTML to create a page output to the browser. During the render event, the page calls them in the page calls them to HTML. The page can then be accessed by the user's browser in the form of HTML. When the render event is overloaded, developers can write custom HTML code make the original HTML to organize the page in accordance with new HTML. The render method uses an HTMLTextWriter object as a parameter and displays HTML in the browser in the form of a web page. At this time, some modifications can still be made, but they are just some changes in the client. You can overload the render event, as follows (Figure 6):
Figure 6
ten. Destroy
After being delivered to HTML, all objects should be destroyed. In the Dispose event, you should destroy all the objects created when this page is established. At this time, all the processing has been completed, so it is destroying any remaining objects that will not generate errors, including page objects. You can overload the Dispose event, see Figure 6. The above is the ten events in the ASP.NET page life cycle. Every time we ask an ASP.NET page, we have experienced the same process: from the initialization object to the destroyable object. By understanding the internal running mechanism of the ASP.NET page, I believe that everyone will be more excited when writing, debugging the code.