Basic Theory:
If the session value is stored in the server memory, it is certain that a large number of sessions will increase the server load. viewstate stores data in the page hidden control and no longer occupies server resources. Therefore, we can save some variables and objects that need to be remembered by the server to viewstate. sesson should only be applied to variables and object storage that need to span pages and are related to each access user. in addition, the session will expire in 20 minutes by default, while the viewstate will never expire.
Data Type:
However, viewstate does not store all. NET data. It only supports string, integer, Boolean, array, arraylist, hashtable, and custom data types.
Other considerations
Of course, everything has two sides. Using viewstate will increase the HTML output of the page and occupy more bandwidth, which requires careful consideration. in addition, because all viewstates are stored in a hidden field, you can easily view the base64 encoded value by viewing the source code. after conversion, you can get the objects and variable values in your storage.
In fact, Asp.net also provides more options for viewstate security. there are two methods to protect viewstate: tamper-proofing and encryption. when talking about anti-tampering, we think of the use of hash code. you can add the following code to the top of the page: Page enableviewstatemac = true. In this way, Asp.net will automatically append a hash code to viewstate. When the page is returned, the server generates a hash code based on the returned viewstate and compares it with the returned hash code, if not, the viewstate will be discarded and the control will return to the initial test state. (by default, Asp.net uses the sha1 algorithm instead of the MD5 Algorithm to generate a hash, but this can be used in the machine. configure machinekey validation = "MD5" in config), and viewstate encryption is simpler, as long. set machinekey validation = "3DES" in config to encrypt viewstate with DES.
Usage
How to access viewstate on the client?
Application Environment
Parameters that are independent of security and have a small amount of data and require long-term operations should be accessed using viewstate.
In WebGIS, various map-related states, such as Zoom, center, layers, and visibility, do not have session expiration issues if viewstate is used for access. Web pages can be used forever, you can even download and save the image. When a map request or IFRAME is sent to a third-party application.
Viewstate usage in Asp.netI saw an article yesterday. The author defined several global variables on his own page. The type is static. However, such a definition may have problems. When a user accesses the table, there will be no problems, however, when a large number of users access the system concurrently, the static global variables on the page may become faulty, then, the author used the session variable to save the data previously recorded by the static global variable. Because the session is a State, it is only related to an access process, in this way, there will be no problems caused by the static type.
In this case, too many sessions are in the system. I always think this is not good, although each session has an ID that does not conflict with each other. ASP. NET introduces viewstate, which records some data values in different PostBack of the same page.
So my solution is to record the previous static global variables in the form of viewstate ["name"], so as to avoid this problem, because my system is used in the LAN, the excessive data volume produced by viewstate is negligible.
Asp.net viewstate usage:
ASP. the viewstate in net is ASP. net is used to save the status value when the web control is returned. when the web form is set to runat = "server", this form will be appended with a hidden attribute _ viewstate. _ viewstate stores the State values of all controls in viewstate.
Viewstate is a field in the control class. All other controls inherit control to obtain the viewstate function. Its type is system. Web. UI. statebag, an object set with a name or value.
When requesting a page, Asp. net serializes the status of all controls into a string, and sends it to the client as a hidden property of the form. ASP.. Net analyzes the form attributes that are returned, and assigns them to the corresponding values of the control. of course, these are all created by ASP.. net.
Define viewstate attributes
Public int pagecount
{
Get {return (INT) viewstate ["pagecount"];}
Set {viewstate ["pagecount"] = value ;}
}
Conditions for using viewstate
To use viewstate, you must have a server form tag (<formrunat = Server>) on the ASPX page ). form fields are required so that hidden fields containing viewstate information can be returned to the server. in addition, the form must also be a form on the server side, so that ASP.. NET page framework to add hidden fields.
The value of enableviewstate in page is true.
The value of the enableviewstate attribute of the control is true.
Reminder:
1. When page callback exists, viewstate is disabled if you do not need to maintain the value of the control.
2. viewstate indexes are case sensitive.
3. viewstate is not cross-page.
4. In order to ensure that the package exists in viewstate, the object must be streamed or typeconverter defined.
5. When the textmode attribute of the control textbox is set to password, its status will not be saved in viewstate, which should be for security consideration.
6. Do not use viewstate when the page is not returned or redirected or when it is redirected to another page (transfer.
7. Be careful with its viewstate when creating a control dynamically.
8. When viewstate of a program is disabled, viewstate of all pages of the program is also disabled.
9. viewstate is continuous only when the page returns itself.
Set viewstate
Viewstate can be set in controls, pages, programs, and global configuration. enableviewstate is true by default. to disable the viewstate function for all pages, you can set enableviewstate to false in program configuration.
This article describes ASP. netviewstate usage.