State Management in ASP.NET

Prove you're a Dev Guru! Take the test now!

ASP.NET is a powerful development platform for web applications, based on Microsoft .NET Framework. Microsoft .NET Framework is a software framework containing coded libraries that provide solutions to common programming problems. Web pages in ASP.NET are based on the HTTP protocol.

HTTP is a 'stateless' protocol, which means that a new instance of web page class is created each time the page is posted to the server. All information associated with the web page and the controls on the web page would be lost for each round trip. For instance, if a user inputs some value on the web page, all those input values will get lost in a round trip from browser to the web server.

To overcome this drawback, ASP.NET includes various state management techniques to preserve data. State management is the process of maintaining state and data of pages, throughout multiple requests for the same or different pages.

ASP.NET offers two types of state management:

1. Server-side state management

This state management feature uses resources on the web server. Below are different server-side state management techniques used by ASP.NET.

Application state

ASP.NET enables you to store information using application state, which is an instance of HttpApplicationState class. Application state is a mechanism for storing global information that is available at all the pages.

The Application state instance is automatically created when a user makes a first request to the web application, and all subsequent users share this state object. Application state exposes the key/value dictionary to store information. Consider the following code for saving and retrieving data from Application state:

//Store data in application state

Application["MyApplicationData"] = "my application data";

//Retrieve data from application state

string myData = Application["MyApplicationData"].ToString();

Session state

ASP.NET enables you to store information using session state – which is an instance of HTTPSessionState class. Session state is a mechanism to store information for each active user of the web application.

Session state comes in two forms, 'in-memory' and 'out-of-memory'. Small web applications use the in-memory session state, whereas large applications or applications with high traffic use out-of-memory. Consider the following code for saving and retrieving value from session state:

//Save information in session state

Session[“MySessionData”] = “my session data”;

//Retrieve value from session state

string mySessionData = Session[“MySessionData”];

2. Client-side state management

This state management feature involves storing data either on the client computer or on the web page. Between round trips, no data is maintained on the server. Below are the various client-side state management techniques employed by ASP.NET:

Cookies

A cookie is a small piece of data that is either stored as a text file on a client machine, or in memory during a client browser session. Cookies contain data that the server sends to the client, along with the output of the page. When a user requests a page, the client sends the information in the cookie along with the request information. In ASP.NET, the HttpRequest object contains a list of HttpCookie objects. Cookies are mainly used to store a small amount of data and can be temporary (that expires with time) or permanent. See the following example for storing and retrieving values from cookies:

//Storing value in cookie

HttpCookie myCookie = new HttpCookie(“myCookie”);

myCookie.Value = “myName”;

Request.Cookies.Add(myCookie);

//Retrieving value from cookie

string myName = Request.Cookies[“myCookie”].ToString();

View state/control state

ASP.NET uses View state for retaining data between multiple requests of the same page.ASP.NET stores view state on client side in encrypted format in a __ViewState, which is a hidden field. You can enable or disable this feature of page and control it by specifying the ‘enableviewstate’ property as either true and false. Consider the following code for storing and retrieving custom information in view state:

//Storing data in view state

ViewState.Add(“myViewStateData”, “my Data”);

//Retrieving data from view state

string myViewStateData = ViewState[“myViewstateData”];

Query string

Query string is the information that is appended to the end of a page URL. A URL appended with query string might look like this:

http://www.websitesolution.com/home.aspx?data=mydata

Consider the following code for retrieving information from a query string:

//Retrieving information from query string

string data = Request.QueryString[“data”];

Hidden fields

A hidden control is a control that does not render anything on the web page in the client's browser. ASP.NET uses the HiddenField control to store information. When a user submits a page, this control is posted to the server, along with other controls in the HttpControl collection. Consider the following code for saving and retrieving values from hidden fields:

//Declaring Hidden field

protected HiddenField hdfMyData;

//Saving data in hidden field

hdfMyData.Value = “my hidden data”;

//Retrieving data from hidden field

string myData = hdfMyData.Value;

State management is one of the most powerful features of ASP.NET. These state management techniques, if implemented correctly, can enhance the user experience by providing better security and reducing bandwidth.