Application of the enableviewstate attribute

Source: Internet
Author: User


Each control (on tag) <asp: DataGrid enableviewstate = "false "? />
Each page (in the Command) <% @ page enableviewstate = "false "? %>
Each application (in Web. config) <pages enableviewstate = "false "? />

1. if. <pages enableviewstate = "false" validaterequest = "false"> </pages> in config. What should I do if enableviewstate is not disabled on a page?

1) when a master page exists:

Protected void page_preinit (Object sender, eventargs E)
{
Page. master. enableviewstate = true;

Page. enableviewstate = true;

}

2) When no master page exists

Protected void page_preinit (Object sender, eventargs E)
{
Page. enableviewstate = true;

}

Or <% @ page enableviewstate = "true "? %>

Enableviewstate attribute: enableviewstate attribute of the control in ASP. NET

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:

<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:

ListBox. Items. Add ("client click button once !");

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.

 

The following steps describe the application of the enableviewstate attribute:
(1) Click the command button (or other operations that lead to a round-trip)
(2) because the enableviewstate attribute of lbltag is true, when you click the command button, the returned server information contains the lbltag status value, that is, the text attribute value labelhellohellohello of lbltag.
(3) Find the source file on the webform1.aspx page on the IIS server
(4) Build the page as the original form, that is, the text attribute value of lbltag is label1
(5) set the text attribute value of lbltag to labelhellohellohello Based on the returned value labelhellohellohello"
(6) perform the following operations: This. lbltag. Text = This. lbltag. Text + "hello"; set the text attribute of lbltag to labelhellohellohellohello.
(7) return the page to the browser
(8) Complete the corresponding operations

If the enableviewstate attribute of lbltag is not set to true, that is, false, perform the following operations:
(9) When you click the command button because the enableviewstate attribute of lbltag is false, the returned server information does not contain the status value of lbltag, that is, the text attribute value labelhellohellohello of lbltag is not returned to the server.
(10) Find the source file on the webform1.aspx page on the IIS server
(11) Build the page as the original form, that is, the text attribute value of lbltag is label1
(12) perform the following operations: This. lbltag. Text = This. lbltag. Text + "hello"; set the text attribute of lbltag to labelhello.
(13) return the page to the browser
(14) complete corresponding operations
 

 

Summary

Enableviewstate specifies whether to send the property value and status of a certain control to the server.

The default value of enableviewstate is true. Setting it to false can reduce the data transfer volume. Of course, if the program logic permits setting it to false.

Enableviewstate can be used for controls or @ page, @ control, and @ master commands. When the enableviewstate setting of the control is different from that of the command syntax, set false.

 

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.