Click here to Skip to main content
11,930,449 members (57,573 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


21 bookmarked

State Management in ASP.NET

, 21 Aug 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
State Management in ASP.NET


This article is all about how to maintain, clear or hold state of your pages in ASP.NET application or in web based applications. In this article, I tried to summarize the concept of state management in brief. (But I am including only client side state Management.)


The agenda for this article will be as follows:

  • State | Overview
  • State | Introduction
  • State | Outline
  • State Management Types
    • Client Side Management
    • Server Side Management
  • State Management | Scenario
  • State Management | Techniques
    • Client Side Techniques
      • View
      • Hidden
      • Cookies
      • Control State
      • Query Strings
    • Server Side Techniques
      • Session State
      • Application State

State | Overview

As we all know:

“Browsers are generally state less.”

Now the question arises here, what actually state less mean??

State less means, whenever we do visit any website, our browser communicates with respective server according to our requested functionality or the request. Browser communicates with the respective server through HTTP or HTTPs protocol.

But after that response, what’s next or what is going to happen when we do visit that website again after closing our web browser??

In this case, HTTP/HTTPs doesn’t remember what website or URL we visited or in different words, we can say it doesn’t hold the state of previous website that we visited before closing our browser, that is called state less.

Now I guess you will have at least an idea of actually what State and state less actually means.

So our browsers are State less.

State | Introduction

In this article, I’ll try to give you a feel of state and actually why we do need states and state management concept in ASP.NET. I’ll take you through several state management techniques along with their respective general case example.

State | Outline

As I told you at the beginning, HTTP is a state less protocol. It just cleans up or we can say removes all the resources/references that were serving a particular request in the past. These resources can be:

  • Objects
  • Allocated Memory
  • Sessions IDs
  • Some URL information
  • Etc.

State Management | Types

In ASP.NET, there are 2 state management methodologies. These are:

Client Side State Management

Whenever we do use client side state management, the state related information will directly get stored on the client side. That particular information will travel back and communicate with every request generated by the user then afterwards provides responses after server side communication.

This architecture is something like that:

Server Side State Management

Server side state management is different from client side state management but the operations and working is somewhat same in functionality. In server side state management, all the information is stored in the user memory. Due to this functionality, there are more secure domains at server side in comparison to client side state management.

The structure is something like that:

State Management | Scenario

It will be a little difficult to directly evaluate what will be better for our application. We cannot directly say that we are going to use client side or server side architecture of state management.

State Management | Techniques

State management techniques are based on client side and server side. Their functionality differs according to the change in state so, here is the hierarchy:

Client Side | Techniques

Client side state management techniques are:

  • View State
  • Hidden field
  • Cookies
  • Control State
  • Query Strings

Server Side | Technique

Server side state management techniques are:

  • Session State
  • Application State

Now, I am defining each and every technique in detail with their reference example.

View State

In general, we can say it is used for storing user data in ASP.NET. Sometimes in ASP.NET applications, users want to maintain or store their data temporarily after a post back. In this case, VIEW STATE is the most used and preferred way of doing this mechanism.

This property is enabled by default, but we can make changes according to our functionality, what we need to do is just change EnableViewState value to either TRUE for enabling it or FALSE for opposite operation.

[View State Management]


// Page Load Event
protected void Page_Load(object sender, EventArgs e)
    if (IsPostBack)
        if (ViewState["count"] != null)
            int ViewstateVal = Convert.ToInt32(ViewState["count"]) + 1;
            View.Text = ViewstateVal.ToString();
            ViewState["count"] = "1";

// Click Event
protected void Submit(object sender, EventArgs e)
Points to Remember

Some of the features of view state are:

  • It is page level state management
  • Used for holding data temporarily
  • Can store any type of data
  • Property dependent

Hidden Field

Hidden field is used for storing small amount of data on client side. In most simple words, it’s just a container that contains some objects but their result does not get rendered on our web browser. It is invisible in the browser.

It stores one value for the single variable and it is the preferable way when a variable’s value is hanged frequently but we don’t need to keep track of that every time in our application or web program.

[Hidden Field Management]


// Hidden Field

int newVal = Convert.ToInt32(HiddenField1.Value) + 1;
HiddenField1.Value = newVal.ToString();
Label2.Text = HiddenField1.Value;
Points to Remember

Some features of hidden fields are:

  • Contains small amount of memory
  • Direct functionality access


Cookie is a small text file that gets stored in users hard drive using client’s browser. Cookies are just used for the sake of user’s identity matching as it only stores information such as sessions ids, some frequent navigation or postback request objects.

Whenever we get connected to the internet for accessing particular service, that cookie file gets accessed from our hard drive via our browser for identifying user. The cookie access depends upon the life cycle of expiration of that particular cookie file.

[Cookie Management]


int postbacks = 0;
if (Request.Cookies["number"] != null)
    postbacks = Convert.ToInt32(Request.Cookies["number"].Value) + 1;
// Generating Response
    postbacks = 1;
Response.Cookies["number"].Value = postbacks.ToString();

Result.Text = Response.Cookies["number"].Value;

Cookie | Types

Persistent Cookie

Cookie having expiration date is called persistent cookie. These type of cookies reach their end as their expiration dates comes to end. In this cookie, we set an expiration date.


Response.Cookies["UserName"].Value = "Abhishek";
Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(1);

HttpCookie aCookie = new HttpCookie("Session");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Non-Persistent Cookie

Non-persistent type of cookie doesn’t get stored in client’s hard drive permanently. It maintains user information as long as user accesses or uses the services. It's simply the opposite procedure of persistent cookie.


HttpCookie aCookie = new HttpCookie("Session");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Points to Remember

Some features of cookie are:

  • Store information temporarily
  • It’s just a simple small sized text file
  • Can be changed according to requirement
  • User preferred
  • Requires only few bytes or KBs of space for creating cookies

Control State

Control state is based on custom control option. For expected results from CONTROL STATE, we need to enable the property of view state. As I already described, you can manually change those settings.

Points to Remember

Some features of query strings are:

  • Used for enabling View State Property
  • Defines custom view
  • View State property declaration
  • Can’t be modified
  • Accessed directly or disabled

Query Strings

Query strings are used for some specific purpose. These in general case are used for holding some value from a different page and move these values to the different page. The information stored in it can be easily navigated from one page to another or to the same page as well.

[Query Strings]


// Getting data
if (Request.QueryString["number"] != null) 
    View.Text = Request.QueryString["number"];

// Setting query string
int postbacks = 0;

if (Request.QueryString["number"] != null) 
    postbacks = Convert.ToInt32(Request.QueryString["number"]) + 1;
    postbacks = 1;

Response.Redirect("default.aspx?number=" + postbacks);
Points to Remember

Some of the features are:

  • It's generally used for holding values
  • Works temporarily
  • Switches information from one to another page
  • Increased performance
  • It uses real and virtual path values for URL routing

I hope you will enjoy reading this article. I’ll update server side state management very soon.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Abhishek Jaiswall
Software Developer
India India
Hello Buddies!
This is Abhishek, a software scientist and a geek who loves to explore and enjoy new technologies in his own ways. He loves every aspect of technology.

You may also be interested in...

Comments and Discussions

QuestionState Menagement in Mails Pin
ravithejag6-Mar-15 1:31
memberravithejag6-Mar-15 1:31 
GeneralMy vote of 3 Pin
Monibrata Bhattacharjee13-Feb-15 6:03
memberMonibrata Bhattacharjee13-Feb-15 6:03 
GeneralRe: My vote of 3 Pin
Abhishek Jaiswall18-Feb-15 1:46
memberAbhishek Jaiswall18-Feb-15 1:46 
GeneralMy vote of 1 Pin
Dmitry Mukalov10-Sep-14 3:11
memberDmitry Mukalov10-Sep-14 3:11 
GeneralMy vote of 3 Pin
Sidd-B21-Aug-14 9:38
memberSidd-B21-Aug-14 9:38 
GeneralRe: My vote of 3 Pin
Abhishek Jaiswall10-Sep-14 7:43
memberAbhishek Jaiswall10-Sep-14 7:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 21 Aug 2014
Article Copyright 2014 by Abhishek Jaiswall
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid