Asp.net viewstate sending-back mechanism

Source: Internet
Author: User

The so-called sending-back mechanism is actually simply sending it to yourself (this page. Next, create a new website and open the Default. aspx page that is added by Default. The Code of form is as follows:
Copy codeThe Code is as follows:
<Form id = "form1" runat = "server">
<Div>
</Div>
</Form>

The following is the HTML code after the page is run:
Copy codeThe Code is as follows:
<Form name = "form1" method = "post" action = "Default. aspx" id = "form1">
<Div>
<Input type = "hidden" name = "_ VIEWSTATE" id = "_ VIEWSTATE" value = "/wEPDwUJNzgzNDMwNTMzZGSWiVUOl9W4QUXb + tXv2k5s4yXFog ="/>
</Div>
<Div>
</Div>
</Form>

We can see that the empty form is becoming richer. The first change is the form itself, <form id = "form1" runat = "server"> is interpreted as <form name = "form1" method = "post" action = "Default. aspx "id =" form1 "> automatically adds the action and method attributes to the form1 form. The default value of method is post, and action points to the page itself. Another change is that a hidden field with id = "_ VIEWSTATE" is added to the form1 form. This is the ViewState to be discussed.
Let's take another example: At this time, we add a Label to the page, a TextBox, and a Button Code as follows:
Copy codeThe Code is as follows:
<Form id = "form1" runat = "server">
<Div>
<Asp: Label ID = "Label1" runat = "server" Text = "Label"> </asp: Label> <br/>
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox> <br/>
<Asp: Button ID = "Button1" runat = "server" Text = "Button" onclick = "button#click"/>
</Div>
</Form>

Then write the following code in button#click:
Copy codeThe Code is as follows:
Protected void button#click (object sender, EventArgs e)
{
Label1.Text + = "hello ";
TextBox1.Text + = "hello ";
}

Now let's add some clicks to Button1. We can see that the Label control and TextBox Control are constantly changing as the clicks increase, this situation is very common and easy to understand in Winform, but ASP. NET is based on HTTP, while HTTP is stateless. That is to say, the server has a amnesia. We have just sent a request to the server, created a page object, and responded to it. However, when the second request arrives, the server will no longer know you (the server will create a new page object, which has nothing to do with the previous one, and then give a response again ). It is not a simple task to achieve the same effect as Winform in the harsh conditions of server amnesia.
After adding an attribute EnableViewState = "false" (disable ViewState) to the pre-compiled command Page, run the Page again and click "Button1" continuously to see what the result is? You will find that the Label control effect remains unchanged, but the TextBox Control content is still changing. Why? In fact, we only need to check the source code and find:
Copy codeThe Code is as follows:
<Div>
<Span id = "Span1"> Label </span> <br/>
<Input name = "TextBox1" type = "text" id = "Text1"/> <br/>
<Input type = "submit" name = "Button1" value = "Button" id = "Submit1"/>
</Div>

The three controls are changed to span, text, and submit, respectively ), when we click Button1, the name and value Attributes of All Form Controls in the form to be submitted in HTML are connected to a name = value string with "&", and then directed to the page of action, method to send the request. You can change the form sending method to get (<form id = "form1" runat = "server"> ">) click Submit again to notice the changes in the address bar.
Because the Label control generates a span Label, but the value in the span Label is not submitted when the form is submitted, but the value in the input is submitted, the server can obtain the latest value of the input, because the value of span is not sent, the server cannot get the latest change of span. This is why the Label does not change when ViewState is disabled, but the input changes. To solve this problem, asp.net introduces ViewState. In fact, ViewState is only a hidden domain. The form does not send the value of the span tag, but sends the value of the hidden control. So when asp.net assigns a value to the span tag, it then records the hidden control named ViewState, in this way, the server can obtain the original content of the Label from hidden.
This is the essence of ViewState. Of course, there are LoadViewState and SaveViewState methods on the server side. If the night is too deep, I will not talk about it any more. This is my younger brother's first blog post. I am afraid to put it on the homepage. I hope everyone can give priority to encouragement and make bricks! Thank you.
I would also like to thank Brother tan, who has benefited a lot from this book "not far from the road-in-depth ASP. NET control development.
Remove viewstate from asp.net

Related Article

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.