For more information, see viewstate.

Source: Internet
Author: User

Okay, I admit, I have a drag-and-drop problem. I wanted to write something at the beginning of the month, but it was delayed until the end of the month .. it's speechless. change from now on, try to get rid of this bad problem!

 

I will dedicate my first blog to viewstate. Let's talk about my understanding of viewstate.

 

In ASP. NET, In order to simulate the Event Response Mechanism in winform, Microsoft engineers are really painstaking and have invented the "sending back" mechanism, making it as easy to compile web pages as winform. The so-called sending-back mechanism is actually simply sending it to yourself (this page. Next we will create a new website, open the default. aspx page that is added by default, in the form SectionCodeAs follows:

 

1 < Form ID = " Form1 " Runat = " Server "   >
2 < Div >
3
4 </ Div >
5 </ Form >

 

 

The following is the HTML code after the page is run:

 

1 < Form name = " Form1 " Method = " Post " Action = " Default. aspx " ID = " Form1 " >
2 < Div >
3 < Input type = " Hidden " Name = " _ Viewstate " ID = " _ Viewstate " Value = " /Wepdwujnzgzndmwntmzzgswivuol9w4quxb + txv2k5s4yxfog = "   />
4 </ Div >
5
6 < Div >
7
8 </ Div >
9 </ Form >

 

 

 

We can see that the empty form is becoming richer. The first change is the form itself, < Form ID = " Form1 " Runat = " Server "   > Interpreted < Form name = " Form1 " Method = " Post " Action = " Default. aspx " ID = " Form1 " > Automatically adds the action and method attributes to the form1 form. The default method is post, and the action points to the page itself. Another change occurs when an ID is added to the form1 form.="_ Viewstate" 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:

1 < Form ID = " Form1 " Runat = " Server "   >
2 < Div >
3 < ASP: Label ID = " Label1 " Runat = " Server " Text = " Label " > </ ASP: Label > < BR />
4 < ASP: textbox ID = " Textbox1 " Runat = " Server " > </ ASP: textbox > < BR />
5 < ASP: button ID = " Button1 " Runat = " Server " Text = " Button " Onclick="Button#click" />
6 </ Div >
7 </ Form >

 

Then write the following code in button#click:

 

1 Protected   Void Button#click ( Object Sender, eventargs E)
2 {
3 Label1.text + =   " Hi! " ;
4 Textbox1.text + =   " Hi! " ;
5 }

 

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.

We add an attribute to the pre-compiled command page.Enableviewstate = "false"(Disable viewstate), re-run the page, and continue to click button1 continuously to see what the effect is? You will find that the label control effect remains unchanged, but the Textbox Control content is still changing. Why? In fact, you only need to checkSource codeYou will find that:

 

1 < Div >
2 < SPAN ID = " Span1 " > Label </ Span > < BR />
3 < Input name = " Textbox1 " Type = " Text " ID = " Text1 "   /> < BR />
4 < Input type = " Submit " Name = " Button1 " Value = " Button " ID = " Submit1 "   />
5 </ 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. We can<Form ID = "form1" runat = "server">Change the form sending method to get(<Form ID = "form1" runat = "server" method = "get">)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 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.

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.