Asp. NET EnableViewState properties of controls and completely disable _ Practical tips

Source: Internet
Author: User

In ASP.net, the WebForm controls are introduced to EnableViewState properties that were not previously. What is the use of this property? We know that for WebForm, its code is on the server side to handle the client's request. When a user browses through a Web page in a browser, something happens to the page, such as opening a new link, or clicking a button. In ASP, these are processed in a scripting language before being passed to the server side. But under ASP.net, because of the use of code behind technology, in coding, usually the work done before the client to the server side.

So how does the server know what the customer is doing? Like what I typed in the text box, or click the login button, how does the server end up with this information? Because there is no such information, the server side is unable to respond to the customer's request. The principle is that ASP.net cites the viewstate mechanism. The state of each control and page of the page is saved on the server side, which includes the layout of each control on the page, and their respective properties. These values are saved under ViewState. We can observe the HTML source code of the ASPX page, assuming that there is a button on the page, and a ListBox control, the HTML file is as follows:

Copy Code code 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 between us is that there is less scripting language that previously had to respond to client events, and one has a property called "_viewstate". The value is a long string of characters. Type is "hidden". This value records the status information of each control and page. When the user is related to the page, the status value is changed and the changed value is passed to the server side. The difference between the changed state value and the initial value of the server side in response to a specific request.

Once the page has a lot of controls, this frequent transfer control state value is a huge drain on the network, so the ASP. NET provides the EnableViewState property and the default value of the system is true. When set to true, the control is included when the state value is passed, and if set to false, it is not included when the state value is passed. Since the status value does not include the control, the server side does not respond to the client's action on it.

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

Copy Code code as follows:
LISTBOX.ITEMS.ADD ("Client click button once!") ");

Run the application at this point, click the button on the page, add content in the ListBox, click Continuously, and add content. If we change the EnableViewState property of the ListBox to False, you can only add one time if you keep clicking the button.

What good is that? If we are developing a Web application, some controls do not need to accept the user's action or need to accept only one operation, we can change the EnableViewState property of these controls to false, so that we can optimize our program to improve the speed of network access.

Extensions: about how to disable EnableViewState completely

Someone on the internet said, "Go to Web.config and EnableViewState from the global." Open an empty page again to see, is not refreshing a lot. Ah? In the page source code, there are still:

Copy Code code as follows:
<input type= "hidden" name= "__viewstate" id= "__viewstate" value= Smryke3ellqkiinbarzxe "/>

Personal understanding, ASP. NET in order to maintain their state, you must use the ViewState. However, there is a way to completely remove ViewState, as follows:

1. First, asp.net4.0 other versions are not tested and interested in testing the other versions.

2. The foreground page displays data only with repeater and literal controls.

3. Using HTML controls, not using standard controls, submitting data based on form, rather than the default event pattern in asp.net.

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

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.