ASP. NET control EnableViewState property and disable completely

  • 2021-07-24 10:50:58
  • OfStack

Introduce EnableViewState properties in ASP. Net that were not previously available for each WebForm control. What is the use of this attribute? We know that for WebForm, the code is on the server side to handle client requests. When a user browses a web page through a browser, some actions will be taken on the web page, such as opening a new link or clicking a button. In ASP, these are processed through a scripting language and then passed on to the server. However, under ASP. NET, due to the adoption of code behind technology, in coding, the previous work completed by the client is usually put into the server.

So, how does the server know the customer's operation? For example, what I entered in the text box or clicked the login button, how did the server get this information? Without this information, the server cannot respond to the client's request. The principle is that ASP. NET refers to the mechanism of viewstate. On the server side, the state of each control of the web page and the page is saved, including the layout of each control on the page and their respective attributes. These values are stored under ViewState. We can look at the html source code for the Aspx page, assuming that there is an button button and an listBox control on this page, and the html file is as follows:

<input type="hidden" name="__VIEWSTATE" value="dDwzODYzNDM5NTU7Oz7FvviJbq45bDa7QJaumIiOhZ8mOQ==" />
<input type="submit" name="Button1" value="Button" id="Button1" style="height:40px;width:96px;Z-INDEX: 101; LEFT: 200px; POSITION: absolute; TOP: 240px" />
<select name="ListBox1" size="4" id="ListBox1" style="width:152px;Z-INDEX: 102; LEFT: 176px; POSITION: absolute; TOP: 120px"></select>

The difference is that 1 is missing the scripting language that must respond to client events before, and 1 is adding a property named "_ VIEWSTATE". Its value is 1 long string of characters. Type is "hidden". This value records the state information of each control and page. When the user performs related operations on the page, the status value changes and the changed value is passed to the server. The server side compares the difference between the changed state value and the initial value to respond to the specific request.

There are many controls in 1 page, and this frequent transfer of control state values consumes a lot of network. Therefore, ASP. Net provides EnableViewState attribute, and the default value of the system is true. When set to true, the control is included when the status value is passed; If set to false, it is not included when passing the status value. Since the state value does not include the control, the server does not respond to the client's actions on it.

We can do an experiment and write code in the Button1_Click event:

ListBox.Items.Add(" Client click button 1 Times! ");

At this point, run the application, click the button on the web page, and content will be added in ListBox. Click again and again, and content will be added again and again. If we change the EnableViewState property of ListBox to false and keep clicking the button, we can only add it once.

What are the benefits of this? If we develop Web application program, some controls do not need to accept the user's operation or only need to accept the operation once, we can change the EnableViewState attribute of these controls to false, which can optimize our program and improve the speed of network access.

Extensions: About how to completely disable EnableViewState

Someone on the Internet said, "Go to Web. config and shut down enableViewState globally". Open another empty page to see if it is much refreshing. Huh? In the source code of the page, it still appears:

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZI7OBu+sMrYke3ELlQKIiNbArzXe" />

Personally, the controls in ASP. NET must use VIEWSTATE in order to maintain their state. However, there is a way to completely remove VIEWSTATE, as follows:

1. First of all, other versions of ASP. NET 4.0 have not been tested. If you are interested, you can test other versions under 1.

2. Only Repeater and Literal controls are used to display data on the foreground page.

3. Use HTML controls instead of standard controls to submit data in a form-based format, rather than the default event mode in ASP. NET.


Related articles: