Planet For Application Life Development Presents
MY IT World

Explore and uptodate your technology skills...

ASP.NET - Managing State

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:

  1. View State

  2. Control State

  3. Session State

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

PropertiesDescription
Item(name)The value of the view state item with the specified name. This is the default property of the StateBag class
CountThe number of items in the view state collection
KeysCollection of keys for all the items in the collection
ValuesCollection of values for all the items in the collection

The StateBag class has the following methods

MethodsDescription
Add(name, value)Adds an item to the view state collection and existing item is updated
ClearRemoves all the items from the collection
Equals(Object)Determines whether the specified object is equal to the current object.
FinalizeAllows it to free resources and perform other cleanup operations.
GetEnumeratorReturns an enumerator that iterates over all the key/value pairs of the StateItem objects stored in the StateBag object.
GetTypeGets the Type of the current instance.
IsItemDirtyChecks a StateItem object stored in the StateBag object to evaluate whether it has been modified.
Remove(name)Removes the specified item.
SetDirtySets the state of the StateBag object as well as the Dirty property of each of the StateItem objects contained by it.
SetItemDirtySets the Dirty property for the specified StateItem object in the StateBag object.
ToStringReturns a String representing the state bag object.