Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

State Management in ASP.NET

Author: Nikki Smith

HTTP is a stateless protocol so you need to learn and use state management techniques to manage the state of your application for multiple users.


Due to the stateless nature of HTTP protocol, it is not possible to know about client’s sequence of requests for application pages. A page is created every time it is requested and the information associated with the page is lost so state management is really an issue. Most of the times it is necessary to remember users that visit websites because they do lot of stuff that needs there information on multiple pages. Also there can be multiple users for the same website.  

State management is the way to save and manage users session related information. For example we might want to save preferences and authorization information for the website for every individual user.

State management techniques are available from the classic ASP time. ASP.NET provides different state management techniques which falls under two broad categories of Client Side State Management and Server Side State Management.
Client Side State Management
When information of the client is maintained on client side, it is Client Side State Management. It can be on client’s computer or in a page. There are five client side techniques.
Cookie is the small piece of data that is stored in the client’s browser or in a text file on client’s computer. It contains information about the client’s preferences and configuration settings. Cookies are used to track the client’s requests to server.
Server sends the information in response to the client’s request for the page and that information is saved in client’s browser. When client requests the page again, this information is sent to the server with the request. Cookies can be disabled by the client. In that case, other state management technique can be used. Cookies are stored in a key-value pair. Below is the code to save and retrieve cookies.
To store a Cookie Value
Response.Cookies["Key"].Value = "Value";
Response.Cookies("Key").Value = "Value"
To retrieve the Cookie Value
if (Request.Cookies["Key"] != null) {
     string Value = Request.Cookies["Key"].Value;
If Request.Cookies("Key") IsNot Nothing Then
     Dim Value As String = Request.Cookies("Key").Value
End If
Query String
In Query String, the information is appended at the end of the URL to pass from one page to another. It is a limited way of manage client’s state as most browsers give permission to use 255 characters in URL. So we can pass limited amount of data in Query String. There is also security issue with query string as we write clear text in URL.
We have to append a key-value pair in URL starting with a Question Mark (?) for the first pair and for every next pair “And” sign (&) is appended before key-value pair. For example, we can append key-value pair as below.
To retrieve query string values
string Value1 = Request.QueryString["Value1"];
string Value2 = Request.QueryString["Value2"];
Dim Value1 As String = Request.QueryString("Value1")
Dim Value2 As String = Request.QueryString("Value2")
Hidden Field
ASP.NET provides Hidden Fields to store page specific information. It does not render visibly in the browser. You can set properties of Hidden Fields like for standard controls. If you want to use Hidden Field value, you must have to submit the page using HTTP POST command because Hidden Field value will not be available in HTTP GET Command. You can set and retrieve Hidden Field value like below.
To set a Hidden Field value
using System.Web.UI.HtmlControls;
HtmlInputHidden HiddenField = new HtmlInputHidden();
HiddenField.Value = "Hidden Field Value";
Imports System.Web.UI.HtmlControls
Dim HiddenField As New HtmlInputHidden()
HiddenField.Value = "Hidden Field Value"
To retrieve Hidden Field value
string HiddenValue = HiddenField.Value;
Dim HiddenValue As String = HiddenField.Value
View State
View State provides the facility to store state for multiple requests of a page. The information is stored in a hidden field called _VIEWSTATE.  This single field contains all the values of page controls. View State can be set for each control or for page level by using EnableViewState property. You can use belew syntax to store and retrieve values from View State Hidden Field. It is also ins the form of Key-Value pair. Cast the value in appropriate type when retrieving value. 
To store value in a View State
ViewState["Key"] = "Value";
ViewState("Key") = "Value"
To retrieve a value from View State
string Value = ViewState["Key"].ToString();
Dim Value As String = CType(ViewState("Key"), String)
Control State
As the name indicates, Control State is used to store the state of an individual control across multiple post backs. Sometimes View State is disabled at the page level that is why ASP.NET provides state management at the control level. Control State is really helpful when you have custom controls and you want to store and manage its state.
Server Side State Management
In server side state management, the information of the client is maintained on the server. There are two types of server side state management techniques.
Application State
Application State allows you to store and mange values for your entire application. Information stored in application state is available for all pages and code in application. Application state is very useful when you have data that changes infrequently. So store data once in application object and use it many times for all your application and it is available for multiple sessions. The information is stored in Key-Value pair.
You have to use Application Lock() and Unlock() methods to store application value to avoid any conflict. You can store and retrieve data in Application object as below. Cast the value in appropriate type when retrieving value.
To store data in Application object
Application["Key"] = "Value";
Application("Key") = "Value"
To retrieve a application object value
string Value = Application["Key"].ToString();
Dim Value As String = CType(Application("Key"), String)
Session State
Session State is used to store state information for individual user. Every user will have its own Session object as oppose to Application object which is for multiple sessions. Session object is also stored as Key-Value pair. You can use below code to store and retrieve session object.
To store data in Session object
Session["Key"] = "Value";
Session("Key") = "Value"
To retrieve a session object value
string Value = Session["Key"].ToString();
Dim Value As String = CType(Session("Key"), String)
Add Article Comment:
Name :
Email Address :
Comments :
<< How to move and copy files in ASP.NET

Disclaimer - Privacy
© 2002-2017