ASP.NET – State management


HTTP is the stateless protocol, so technologies use this protocol need to maintain state using other kind of mechanism as it own. ASP.Net is also one of the technologies using HTTP. But it has been bundled and practiced with many technique or mechanism to maintain the state as per the application need. The state management of the ASP.NET explained as below with some simple scenario.

State maintenance in Stateless technology:

Application variable: (System.Web)

Application variable load once when start the projects. It can be used through out application and all the session.

void Application_Start(object sender, EventArgs e)


// Code that runs on application startup

// Application variable initialization in Global.aspx

Application.Add(“AppStartTime”, DateTime.Now.ToString());


Global page application startup event used to initiate the application variable, Application startup event in the global page fired once when application start.

protected void Page_Load(object sender, EventArgs e){// Access the application variable in the web_page.aspx
// Consecutive Response.Write make performance issue
// Strig concatenate using + or += make performance issue

System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(“Application Variable :”);


Output in IE:

Application Variable :4/21/2010 10:36:36 PM

Session variable: (System.Web)

Session variable initiated when user session started, it can be used through out the application, but specific to the session. Unique session ID will created and maintained by the ASP.Net application.Global page session events are taking vital part with user session.

void Session_Start(object sender, EventArgs e)


// Code that runs when a new session is started

// Session variable initialization in Global.aspx

Session.Add(“SessionStartTime”, DateTime.Now.ToString());


protectedvoid Page_Load(object sender, EventArgs e)


// Access of the session variable and Session ID in web_ page.aspx

System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(“Session ID :”);

sb.Append(“Session Variable :”);



Output IE:

Session ID :hco2fsrtxa4ke1i53tsyt0mg
Session Variable :4/21/2010 10:38:58 PM

Session Mode:

We can configure session with different type of session mode as per our need.

In-proc Session Mode: Session maintain within the process is called inproc session mode. That means session variable stored in the ASP.Net worker process in the memory of the server– suitable for simple web application running in single web server with one or more clients architecture.

SQL Server Session Mode: Session maintain in the separate SQL Server database is called SQL Server Session Mode. Session would not be lose when application restarted, it will preserved with SQL Server – Suitable for application running in more than one web server with one or more clients architecture such as web farm or web garden.

State service session Mode : Session maintain in ASP.Net State Services,  This session mode also suitable for web farm and also preserved  when application restarted.

Custom mode: allow us to use own process to main the state.

Off mode: it helps to disable the session state.


mechanism to store pieces of informations in client computer in specific folder for later access. For security reason the sensitive information not recommended to store in cookies.

//Add cookies using HttpCookie collection

HttpCookie hc = newHttpCookie(“MyCookies”);

hc[“Key1”] = “Value1”;

hc.Expires = DateTime.Now;


//Add key and value using Response.Cookies

Response.Cookies[“MyCookies”][“Key2”] = “Value2”;

//Access the cookie

Response.Write(Response.Cookies[“MyCookies”][“Key1”].ToString() + “<br>”);

Response.Write(Response.Cookies[“MyCookies”].Expires.ToLongDateString() + “<br>”);


Query String:

(Postback url with key and value format multiple key, value may concatenate with & )

<asp:LinkButtonID=”LinkButton1″runat=”server”PostBackUrl=”~/Default2.aspx?TestString=Hello World”>LinkButton</asp:LinkButton>

Web address:

http://localhost:1583/Master_websit/Default2.aspx?TestString=Hello World

protectedvoid Page_Load(object sender, EventArgs e)




Output in IE:

Hello World

Hidden Variable: (Web Control)

It is not expired like Session variable, it can be used multiple time within the page.

<asp:HiddenFieldID=”HiddenField1″runat=”server”Value=”I am Hidden Variable”/>

For security reason sensitive information not recommended to store in this mechanism.

View source value is not readable format:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTUxMjM5NDkwOWRkEoalcD2IPs+blVerG2CYaPGTq1E=" />

For example to access the Hidden variable using Page_Load event

protectedvoid Page_Load(object sender, EventArgs e)


Response.Write(“Hidden Variable :” + HiddenField1.Value);


Output in IE:Hidden Variable :I am Hidden Variable

View State: (Statebag of control)

Default mechanism store the information within the page

// Content page load fire first

protectedvoid Page_Load(object sender, EventArgs e)


ViewState.Add(“Textbox1”, TextBox1.Text);

if (IsPostBack == true)


string txb = ViewState[“Textbox1”].ToString();

Response.Write(“Object and value from view state :” + txb);



// Control event fire next these two events

protectedvoid Button1_Click(object sender, EventArgs e)


ViewState.Add(“Textbox1”, TextBox1.Text);


Profile Properties:

Profile properties use to maintain the state specific to the user like session, but it retain for future would not be lost when application restart, because it is not stored in the application memory.

When we create the profile property, we have to specify the provider first, and then we have created meaningful name tag for the property. This property information stored in the assigned provider (SQLProfileProvider is the native provider, we can use custom provider when we need to use XML, FaxPro or Oracle as data source to store the profile properties).  Profile properties are able to manipulate using some of the .Net Classes.

It is very useful when need to maintain user specific information like look and feel such as skin, profile photo etc… , last visited info such as date, time, how long and last accessed module etc..

More information :


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s