Introduction to ASP. NET internal mechanism (6)

Source: Internet
Author: User

Introduction to ASP. NET internal mechanism (V)The lifecycle of the page is omitted here.

Everyone has heard that views in ASP. NET are also used. What is viewstate in essence? Why is ASP. NET unavailable? Let's talk about it now...

We are generally developing object-orientedProgramWe use objects to save data, such as object attributes and fields. When an object method is called, the object state can be changed. After an object is released, its status disappears. Everyone is familiar with this. There is no problem when developing a desktop program, because the objects are generally stored in the local memory until they are no longer used, we will destroy them. However, in web programs, the problem is big. For example, when we request a page, we want to save the status of the previous page, because the status of the previous page is used for the next page. A common example is to compare the user information with the information on the previous page. If the information is different, and so on. However, once the page is handed over to the server, the status information of the last page is lost (HTTP is stateless ).

Therefore, the ASP. Net view mechanism can save the object State at the end of the request, and load the previous state at the beginning of the next object request. The next request is loaded according to the following steps:

1. Create a new object of the same type as the deregister object at the end of the previous request. For example, the first request is default. ASPX page, on the server side, Asp. net will compile default. after a series of actions on the ASPX page, there will be a default_aspx class instance on the server side. After a series of actions, we will take the default request. the data on the ASPX page is finally sent to our browser in HTML format. In this case, the default_aspx instance on the server is destroyed and the object is no longer available. After entering the relevant information in the browser, submit the default request again. when the ASPX page is accessed, the server re-compiles default. on the ASPX page, our data is processed after a series of actions. Send the processing result to us again, and then the default_aspx instance is destroyed again.

Assume that the third request is still default. on the ASPX page, we submit the data again. However, this default _ aspx instance only processes the data we submitted this time and does not care about the data we submitted earlier. More commonly, the data we submit for the second time is a, and the data we submit for the third time is B, in addition, we hope that the page will send the C data to us after comparing the data of A and B, but this processing page is only the data submitted this time, that is, B. So we will never get the result of C.

2. Load the status of the old object for the newly created object.

Only in this way can we get the data C we want. Let's look at the internal mechanism. At the end of each request (simply think that the data is still on the server before the server sends the data to us), some events will be triggered and some methods will be called:

2.1 The page automatically calls the saveviewstate method of controls in the controls collection, that is, all controls in the iteration page, and calls their saveviewstate method, because the controls are subclasses of control, and the control class has this method.

2.2 The saveviewstate method of each control stores the control state to an object.

2.3 The page gets the object in the Saved state returned by each control, and then converts the object in the Saved state to a string using the object-related converter, then, these string expressions are combined and saved in a hidden field. The hidden field name is _ viewstate. as follows:

     input   type ="  hidden  " name  ="  __viewstate  " id  =" __ viewstate  " value  = "/ wepdwullte3mdu5mjy   interval   callback   j4rjagrwsivytp7nqfm  "/>   


2.4 then the _ viewstate hidden field is sent to the client, along with the page data.

2.5 when the page is uploaded back to the server, the following operations occur:

The page string obtains the string expression from _ viewstate.

String expression for parsing page Mining

The page framework uses an object-related type converter to recreate a State object based on a string expression.

The page Framework calls the loadviewstate method of each object in the controls collection to load the previous status.

Then perform other operations.

In this way, ASP. NET saves the status between requests.

Conclusion: The server serializes the object status on the page to the client, and then deserializes the object to get the previous status.

The next section describes the parsing process on the page.

Original article address

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.