asp. net viewstate postback mechanism

  • 2020-05-09 18:29:49
  • OfStack

The so-called postback mechanism, in fact, is to send itself (this page). Now let's create a new website and open the Default.aspx page added by default. The code of form is as follows:
 
<form id="form1" runat="server" > 
<div> 
</div> 
</form> 

Here is the HTML code after running the page:
 
<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 form form itself has been enriched. The first thing that has changed is the form form itself. < form id="form1" runat="server" > Be interpreted as < form name="form1" method="post" action="Default.aspx" id="form1" > , form1 automatically adds the action and method attributes to the form1 form. method is post by default, and action points to the page itself. Another change is to add a hidden field of id=" s 26en "to the form1 form. This is the ViewState we are going to discuss
Let's take another example: at this point, we add 1 Label, 1 TextBox and 1 Button to the page 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 Button1_Click:
 
protected void Button1_Click(object sender, EventArgs e) 
{ 
Label1.Text += " hello "; 
TextBox1.Text += " hello "; 
} 

Now let's add a few clicks to Button1. As you can see, the content of the Label and TextBox controls is constantly changing as we get more and more clicks. This situation is very common and easy to understand in Winform, but ASP.NET is based on the HTTP protocol, while HTTP is a stateless protocol. In other words, the server has amnesia and has just sent him a request, created an page object, and responded. But by the time the second request arrives, the server will no longer recognize you (the server will create a new page object that has nothing to do with the previous one and will respond again). In this kind of server has the bad condition of amnesia, to achieve Winform1 like effect, really is not a simple thing.
After adding a property EnableViewState="false" (ViewState disabled) to the precompiled command Page, rerun the page and continue to click Button1. What does this look like? You will find that the effect of the Label control has not changed, but the content of the TextBox control continues to change. What's the reason for that? In fact, we only need to look at the 1 source code will find:
 
<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 become span, text box (text) and submit button (submit), respectively. When we click Button1, HTML will connect the name and value properties of all the form controls in the form to be submitted with "&" to form a string of name=value, and then send a request to the page pointed to by action, method specified. We can put < form id="form1" runat="server" > The way the form is sent is changed to get ( < form id="form1" runat="server" method="get" > Click submit, and you'll notice the change in the address bar.
Since the Label control ultimately generates a label of span, but when the form is submitted, value in the span tag will not be submitted, but value in input will be submitted. Therefore, the server can get the latest value of input. However, since value of span is not sent, the server cannot get the latest modification of span. This is why when ViewState is disabled, Label does not change, while input does. To solve this problem, asp.net introduces ViewState, but ViewState is just a hidden hidden domain. The form does not send value for the span tag, but value for the hidden control, so asp. net assigns a value to the span tag, and then gives the hidden control, ViewState, another copy of the hidden control, so that the server can get the original content of the Label tag from hidden.
This is the essence of ViewState, and of course there are also LoadViewState and SaveViewState methods on the server side. This is little brother's first blog, dare to put the home page, I hope you can be encouraged to give priority to, brick as auxiliary! Thank you thank you
Thanks to brother tan, I benefited a lot from the book "the road is not far away people -- in-depth ASP. NET control development".
asp net remove viewstate

Related articles: