View state is the method by which the ASP.net page framework saves page and control values between round trips by default. When the HTML form of the page is rendered, the current state and value of the page that needs to be retained during postback are serialized as BASE64 encoded strings and exported to the hidden fields in view state.
View state is a repository in a ASP.net page that stores the values that need to be preserved during postback. For example, you can store information in view state, and the next time the page is sent to the server, the information is accessed during the page load event.
You can access view state information by using the page's ViewState property, which exposes a Dictionary object. Because view state data is stored as strings, only objects that can be serialized are stored.
View state data is stored in one or more hidden fields in the format of a Base64 encoded string. You can access view state information by using the page's ViewState property, which exposes a Dictionary object. Because view state data is stored as strings, only objects that can be serialized are stored.
Because view state is sent as a hidden field, you can make changes to the views state until the Prerendercomplete event occurs. Once the page is rendered to the browser, changes to the view state cannot be saved.
If you view the page output source, you can see the information in the Hidden view state field, which can create a security issue. To mitigate this problem, you can encrypt view state by setting the viewStateEncryptionMode property in the @ Page directive to "Always".
Attention
To use the ViewState property, the ASP.net Web page must contain a FORM element with the runat= "Server" property.
Data types that can be stored in view state
You can store the following types of objects in view state:
String
Integer
Boolean value
Array Object
ArrayList objects
Hash table
Custom type converters (see TypeConverter classes)
You can also store other types of data, but you must compile the class with the Serializable property so that view state can serialize the data into XML.
Considerations for using View state
View state provides status information for a specific asp.net page. If you need to use information on more than one page, or if you need to retain information when you visit a Web site, you should use another method, such as application state, session state, or personalization, to maintain the state.
The view state information is serialized into XML and encoded using BASE64 encoding, which generates a large amount of data. When the page is posted back to the server, the contents of the view state are sent as part of the page postback information. If view state contains a large amount of information, it can affect the performance of the page.
Another important consideration is that if the amount of data in a hidden field is too large, some agents and firewalls will prevent access to the page that contains the data. Because the maximum number varies depending on the firewall and the agent used, a large number of hidden fields can cause occasional problems. To help avoid this problem, if the amount of data stored in the ViewState property exceeds the value specified in the page's Maxpagestatefieldlength property, the page splits the view state into several hidden fields to reduce the size of each individual field below the size of the firewall rejection.
Some mobile devices are not allowed to use hidden fields at all. Therefore, view state is not valid for these devices.