For viewstate, we have to say that it is loaded from Basil)

Source: Internet
Author: User

I always thought that viewstate stores all the page values, and a lot of Asp.net books also said this, or I was stupid and did not understand it until recently I was working on dynamic generation controls, for more information, see viewstate.

Before I explain it, let's talk about the two tools I used: viewstatedecoder (winform), and a webform called viewstate parser. Unfortunately, Paul Wilson does not provide a connection, I did not find it. The other is tcptrace, which is used to view HTTP client requests and server responses (there is a similarProgramIehttpheaders, but it seems that you cannot view the server response)

There is also a commonly used function serverutility. Decode (string s). Because tcptrace encode the client request, we should decode it before placing it in viewstatedecoder.

Before proceeding, I would like to briefly explain the HTTP client requests and server responses. Simply put, the client sends a request and the server returns a response based on the request. Next we will use tcptrace to view the request and response content.

Let's look at a simple HTML page.

<Body>
<Form action = http: // localhost: 8080/method = "Post">
<Input type = "text" name = "TXT" value = "test">
<Input type = "Submit" value = "Smit">
</Form>
</Body>

Before running this page, we need to enable tcptrace for listening. tcptrace can follow the default settings (Listen port: 8080; destination port: 80; server: localhost. Here, we will explain the Form Action Settings: Set to localhost: 8080, which indicates that we have listened to port 8080 of our local machine (the local machine can be used as a server after IIS, Apache, and tomcat are installed ), because we only want to view client requests here, the specific file of action can be not specified (1 ).

However, when you click submit on the page (Make Sure That tcptrace has started listening), the request content is displayed in tcptrace (2)

We can see that the request content includes the Form Content key-value pairs.

Let's take a look at the situation on the ASPX page, as mentioned in some articles on tomato and eggs help you to understand ASP. NET viewstate.

Viewstate is not used to restore the value of form (here form refers to a common form used to submit data to the server.
For example, Textbox, submit, checkbox, select, and so on in input. Because these controls implement the ipostbackdatahandler interface, their values are not saved in viewstate, so viewstate is not used for restoration.

Here I will illustrate it in the form of an example. For the simplest example, note that the suffix of the file here is Aspx.

<Form ID = "form1" method = "Post" runat = "server">
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "button"> </ASP: button>
</Form>

We should know that an important mechanism of the Asp.net page is self-submission. Therefore, we examine the client request content after self-Submission of the page and the server response content.

The post content of the client request after submission includes two name-value pairs _ viewstate and textbox1. Use the urldecode function we mentioned earlier to decode the post content or directly use the content of the server response to hide the content of the _ viewstate field. We can see it through viewstatedecoder.

The textbox content is included in viewstate. In this way, it seems that there is some conflict with that mentioned in tomato and eggs. Let's take a look at it again.EnableviewstateSet to false. The result is displayed after running.

It is found that the textbox content is no longer stored in viewstate, but we can see that after the submission, the textbox value is still displayed on the page. It can be seen that the textbox value is indeed not controlled by viewstate. In fact, textbox inherits the ipostbackdatahandler interface. Through reflector, let's take a look at the textbox'sSource code

Bool ipostbackdatahandler. loadpostdata (string postdatakey, namevaluecollection postcollection)
{
String text1 = This. text;
String text2 =Postcollection[Postdatakey];
If (! Text1.equals (text2 ))
{
This. Text = text2;
Return true;
}
Return false;
}

The name-Value Pair stores the names (note that the name is not the ID) and value of all labels marked as runat = "server" on the page, that is, in addition to the _ viewstate content of the Self-submitted client post, we found the value in loadpostdata through postdatakey, that is, the name of the tag to fill in the value.

You may wonder why name rather than ID is required here. In fact, this is related to the http post method. Remember that the HTML page we discussed earlier must use name instead of ID. Let's take a look at the addattributestorender method of testbox.

Writer. addattribute (htmltextwriterattribute. Name, this. uniqueid );

It can be seen that the name is assigned a value here. Therefore, we find that viewstate is not required here.

Conclusion:

With this understanding, we can reduce the viewstate size to a greater extent. With viewstatedecoder and tcptrace, we can observe the changes in viewstate, which is very helpful for developing dynamic controls.

Note:

(1) If the method is post and the action is static, such as htm, the following error is reported:

405 error. The resource is forbidden. The specific server response is

HTTP/1.1 405 method not allowed

Server: Microsoft-Microsoft IIS/5.1

Date: Tue, 20 Sep 2005 09:18:31 GMT

Connection: Close

Allow: Options, Trace, get, head, put, delete

This should be related to the server settings.

(2) If we only want to determine whether the page exists, rather than getting all the server responses, we can write

XMLHTTP = new activexobject ("Microsoft. XMLHTTP ")
XMLHTTP. Open ("head", URL, false)
XMLHTTP. Send ()

This article introduces the network traffic and increases the speed. For writing in other languages, it is not provided here because of the time. Please search for it by yourself.

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.