HTTP ( Hyper Text Transfer Protocol) is a stateless protocol. When the client disconnects from the server, the ASP.Net engine discards the page objects. This way each web application can scale up to serve numerous requests simultaneously without running out of server memory.
However, there need to be some technique to store the information between requests and to retrieve it when required. This information i.e., the current value of all the controls and variables for the current user in the current session is called the State.
ASP.Net manages four types of state:
View State
Control State
Session State
Application State
View State:
The View State is the state of the page and all its controls. It is automatically maintained across posts by the ASP.Net framework.
When a page is sent back to the client, the changes in the properties of the page and its controls are determined and stored in the value of a hidden input field named _VIEWSTATE. When the page is again post back the _VIEWSTATE field is sent to the server with the HTTP request.
The view state could be enabled or disabled for:
The entire application - by setting the EnableViewState property in the <pages> section of web.config file
A page - by setting the EnableViewState attribute of the Page directive, as <%@ Page Language="C#" EnableViewState="false" %>
A control - by setting the Control.EnableViewState property.
It is implemented using a view state object defined by the StateBag class which defines a collection of view state items. The state bag is a data structure containing attribute/value pairs, stored as strings associated with objects.
The StateBag class has the following properties:
Properties | Description |
Item(name) | The value of the view state item with the specified name. This is the default property of the StateBag class |
Count | The number of items in the view state collection |
Keys | Collection of keys for all the items in the collection |
Values | Collection of values for all the items in the collection |
The StateBag class has the following methods
Methods | Description |
Add(name, value) | Adds an item to the view state collection and existing item is updated |
Clear | Removes all the items from the collection |
Equals(Object) | Determines whether the specified object is equal to the current object. |
Finalize | Allows it to free resources and perform other cleanup operations. |
GetEnumerator | Returns an enumerator that iterates over all the key/value pairs of the StateItem objects stored in the StateBag object. |
GetType | Gets the Type of the current instance. |
IsItemDirty | Checks a StateItem object stored in the StateBag object to evaluate whether it has been modified. |
Remove(name) | Removes the specified item. |
SetDirty | Sets the state of the StateBag object as well as the Dirty property of each of the StateItem objects contained by it. |
SetItemDirty | Sets the Dirty property for the specified StateItem object in the StateBag object. |
ToString | Returns a String representing the state bag object. |