EnableViewState attribute of the control in ASP. NET and disable it completely,
In ASP. Net, the previous EnableViewState attribute is introduced to each WebForm control. What is the purpose of this attribute. We know that for WebForm, the code is on the server side to process client requests. When a user browses a webpage through a browser, the user performs some operations on the webpage, such as opening a new link or clicking a button. In ASP, these are processed by the script language, and then passed to the server. However, in ASP. NET, because code behind technology is used, in coding, the work previously completed by the client is usually put on the server.
So how does the server know the customer's operations? For example, if I enter content in the text box or click the login button, how does the server obtain the information? Without this information, the server cannot respond to the customer's request. The principle is that ASP. NET references the viewstate mechanism. The server stores the status of various controls and pages on the webpage, including the layout and attributes of each control on the page. These values are stored in ViewState. We can observe the html source code of the Aspx page. Suppose there is a button and a listBox control on this page. The html file is as follows:
Copy codeThe Code 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, first, the scripting language that must respond to client events is missing, and the other is the attribute named "_ VIEWSTATE. The value is a long string of characters. The type is "hidden ". This value records the status information of each control and page. When you perform operations on the page, the status value changes and the changed value is passed to the server. The server compares the differences between the changed status value and the initial value to respond to specific requests.
Once there are many page controls, this frequent transfer of control status values will consume a lot of network resources. Therefore, ASP. Net provides the EnableViewState attribute, and the default value is true. When it is set to true, this control is included when the status value is passed; if it is set to false, it is not included when the status value is passed. Since the status value does not include this control, the client does not respond to the operation on it.
We can make an experiment and write the code in the button#click event:
Copy codeThe Code is as follows: ListBox. Items. Add ("click the button once on the client! ");
In this case, run the application, click the button on the webpage, and add the content to The ListBox. Click the content to add the content. If we change the EnableViewState attribute of ListBox to false and click the button continuously, we can only add it once.
What are the benefits? When developing a Web application, you can change the EnableViewState attribute of some controls to false if they do not need to be operated by the user or only need to be operated once, this will optimize our programs and speed up network access.
Extended: How to completely disable EnableViewState
Someone on the Internet said, "Disable enableViewState from global in Web. config ". Open an empty page to see if it is refreshing. Ah? In the page source code, the following still appears:
Copy codeCode: <input type = "hidden" name = "_ VIEWSTATE" id = "_ VIEWSTATE" value = "/wEPDwULLTE2MTY2ODcyMjlkZI7OBu + sMrYke3ELlQKIiNbArzXe"/>
Personally, VIEWSTATE is required for controls in ASP. NET to maintain their statuses. However, there is a way to completely remove VIEWSTATE, as shown below:
1. ASP. NET4.0 is not tested in other versions. If you are interested, you can test other versions.
2. Only the Repeater and Literal controls are used to display data on the front-end page.
3. Use HTML controls, do not use standard controls, submit data in form, rather than the default Event Mode in ASP. NET.
The above is all the content of this article. I hope you can give us a reference and support the customer's home.