The so-called postback mechanism, in fact, is to send themselves to their own (this page) just. Below we create a new Web site, open the default added Default.aspx page, the form part of the code is as follows:
Copy Code code as follows:
<form id= "Form1" runat= "Server" >
<div>
</div>
</form>
The following is the HTML code after the page is run:
Copy Code code as follows:
<form name= "Form1" method= "post" action= "default.aspx" Id= "Form1" >
<div>
<input type= "hidden" name= "__viewstate" id= "__viewstate" value= txv2k5s4yxfog== "/>
</div>
<div>
</div>
</form>
We can see the original empty form form become rich, the first change is the form form itself, <form id= "Form1" runat= "Server" > was interpreted as <form name= "Form1" Method= "POST" action= "default.aspx" Id= "Form1", automatically gives Form1 this form adds action and method attributes, and method defaults to Post,action to the page itself. Another change is that the Form1 form adds a id= "__viewstate" hidden field, which is what we're talking about VIEWSTATE
Let's take another example: At this point, we add a label to the page and a textbox with a button code as follows:
Copy Code code 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= "Button1_Click"/>
</div>
</form>
Then write the following code in the button1_click:
Copy Code code as follows:
protected void Button1_Click (object sender, EventArgs e)
{
Label1.Text = "Hello";
TextBox1.Text = "Hello";
}
Okay, now we're going to add some clicks to the Button1 and see that the label and TextBox controls are constantly changing as we click, which is very common and easy to understand in WinForm, However, the ASP.net is based on the HTTP protocol, and the HTTP protocol is a stateless protocol. In other words, the server has amnesia, clearly just recently sent him a request, created a Page object, and respond. But the second time the request arrives, the server doesn't know you anymore (the server creates a new Page object that has nothing to do with the previous one, and responds again). In this kind of server has the bad condition of amnesia, want to achieve winform the same effect, really is not a simple thing.
After adding a property enableviewstate= "false" (disable viewstate) to the precompiled instruction page, rerun the page, continue clicking Button1, and see what the effect is? You will find that the label control effect is unchanged, but the contents of the TextBox control continue to change. What's the reason? In fact, we just need to look at the source code will find:
Copy Code code 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 3 controls become span, text box, and submit button (submit), and when we click Button1, HTML will connect the name and value properties of all form controls in the form that will be submitted with "&" to name=value strings. Then send the request to the page that the action points to, method specifies. We can modify the <form id= "Form1" runat= "Server" > Form delivery to get (<form id= "Form1" runat= "Server" method= "get" >) and click Submit, You'll notice the change in the Address bar.
Because the label control eventually generates a span, the form does not commit value in the span tag when it commits, but the value in input is submitted, so the server can get the latest value of input, and because no span value is sent, So the server cannot get the latest changes to span. This is why when you disable ViewState, the label does not change, and input is the cause of the change. To solve this problem, ASP.net introduced ViewState, in fact, ViewState is a hidden hidden domain just. The form does not send the value of the span tag, but it sends the value of the hidden control. So when asp.net the span tag, it records a copy of the hidden control named ViewState so the server can get the original label label from Hidden.
This is the essence of viewstate, of course, in the server side there are LoadViewState and SaveViewState methods, late at night, no longer said. This is the younger brother's first blog, ventured into the home page, I hope everyone can be encouraged to take the main, Pat Brick as a supplement! Thanks thanks
Also thanks to Brother Tan, that book "Tao not far person--deep asp.net control development" Let me benefit a lot.
asp.net removal viewstate