I. Definition
Page lifecycle refers to the process from initialization to destruction of an ASP. NET page object.
Once a request maps to an ASP. NET page, an object that inherits directly or indirectly from system. Web. UI. Page will start initialization.
And start to execute all the steps in the page lifecycle.
This object is the background hosting of the ASP. NET page requested by the client.Code
You can add trace attributes in the <% @ page/%> command on the page.
Preinit initcomplete
Loadstate processpostdate preload load processpostdata
Changedevents postbackevent
Savestate savestatecomplete render
2. Functions of each step
1). Initialization
Priinit, init, and initcomplete
Function: initializes class objects, initializes the main page of a topic, and determines whether the object is accessed for the first time.
2). load data and pages
Loadstate, processpostdate, preload, load, processpostdata (second time), etc.
First, load the viewstate object from the data returned by the page. All the data is base64-encoded and transmitted to the server
Then process the returned data, that is, store the key/value pairs in the form to the object.
Then load the page,ProgramDevelopers usually perform initialization programming here, such as compiling page initialization code in the onload event.
Finally, execute processpostdata again to process the newly added data during load.
3). Trigger the event
Contains the changedevents and postbackevent steps.
First, compare the data in viewstate with the data returned from the last page to find out which events need to be triggered. Here the events are triggered one by one.
The order cannot be determined.
4) Save the status and display the page
Savestate savestatecomplete render step
First, the page will encode and save all viewstate data
Then, embed it into a hidden control of the page.
Then, convert all the control labels, generate the page HTML, and send it back to the client.
3. viewstate Mechanism
1). Overview
ASP. NET Server Control from system. web. UI. control (defined by all ASP. the properties, methods, and events shared by the. NET Server Control are inherited by the viewstate read-only attribute, which adds the status management mechanism. The dictionary used to obtain status information. This information allows you to save and restore the viewstate of the server control among multiple requests on the same page. The viewstate type of the server control is system. web. UI. statebag is a dictionary that stores key/value pairs.
2) viewstate Implementation Mechanism
Simply put, each time the server sends a page to the client, a data is hidden and used to save the viewstate. When the client submits the form again, the viewstate will be submitted to the server with the form again, and the function of continuously saving data is achieved in the loop.
Procedure
When the customer applies for a new page with the viewstate field, the data in the viewstate field is blank during the first application.
The customer submits the form. The viewstate field is submitted as part of the form. It is blank.
The server reads viewstate data from the form, decodes and decrypts the data, and stores the data in the viewstate object. The first time it is still blank
After the server processes the form data, it writes the form data to the viewstate object. Then, the viewstate object has a value.
The server encrypts and encodes the viewstate object and then writes it to the viewstate field of the page. The viewstate field on the page contains
Control Data and server code written into viewstate data in the form submitted last time
The server returns the page to the client, and the client obtains the viewstate data.
Because the page class inherits from the templatecontrol class and the templatecontrol class inherits from the control class, you can directly use the viewstate attribute.
This function is extremely useful after the viewstate of the control is disabled.
Using System; Using System. Data; Using System. configuration; Using System. Web; Using System. Web. Security; Using System. Web. UI; Using System. Web. UI. htmlcontrols; Using System. Web. UI. webcontrols; Using System. Web. UI. webcontrols. webparts; Public partial class _ Handler : System. Web. UI. Page ;{ Protected void Page_load ( Object Sender, Eventargs E ){ If (! Page. ispostback ){ // Initialize the value Viewstate [ "Value" ] = 0; output ();}} Protected void Go_click ( Object Sender, Eventargs E ){ // Increase the value by 1 Viewstate [ "Value" ] = ( Int ) Viewstate [ "Value" ] + 1; output ();} Private void Output (){ // Display the ASP: Label control with output as a page Output. Text = viewstate [ "Value" ]. Tostring ();}}
3). Substance
ASP: the label control is actually a class of system. Web. UI. webcontrols. webcontrol. label. Because the webcontrol class inherits from
The system. Web. UI. control class, and the control class has the viewstate attribute. Therefore, it is actually a class that uses the viewstate mechanism.
ViewSource codeGenerally, an input similar to the following is displayed.
The value is base64-encoded. The value is essentially an XML file, saving all viewstate objects.
<Input type = "hidden" name = "_ eventvalidation" id = "_ eventvalidation" value = "/wewagk + heeoz767wdfmnbml0vtvt5dztfewg6rrwb7b7jc"/>
ASP: textbox controls directly implement the <input type = "hidden"/> mechanism and are not affected by the viewstate mechanism.