---
title: "State Management in ASP.NET"  
description: "State Management in ASP.NETState management is the process by which you maintain state and page information over multiple requests for the same or dif"  
author: "Chris Anderson"  
published: 2011-08-08  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/219/state-management-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 3 minutes  

---

# State Management in ASP.NET

[State Management](https://www.mindstick.com/forum/296/state-management-in-asp-dot-net) in ASP.NET

State management is the process by which you maintain state and page information over multiple requests for the same or different pages.

## Types of State Management

There are 2 types State Management:

## Client – Side State Management

This stores information on the client's computer. The [techniques available](https://answers.mindstick.com/qa/96750/what-are-software-project-estimation-techniques-available) to store the state information at the client end are listed down below:

**a) View State:** ViewState is the mechanism that allows state values to be preserved across page postbacks. Values stored in view state will loss with the page.

### Example:

ViewState ["clickCounter"] = 1; //set value in ViewState field

int value = (int) ViewState ["clickCounter"]; //get value from ViewState field

**b) Hidden fields:** – Like view state, hidden fields store data in an HTML form without displaying it in the user's browser. The data is available only when the form is processed.

### Example:

<input type="hidden" runat="server" id="hdnValue" /> //creating hidden field

hdnValue.Value="admin"; //set value in hidden field

string value=hdnValue.Value; //get value from hidden field

**c) Cookies:** Cookies store a value in the user's browser that the browser sends with every page request to the same server. Cookies are the best way to store state data that must be available for multiple [Web pages](https://www.mindstick.com/blog/367/transferring-data-between-asp-dot-net-web-pages) on a [web site](https://www.mindstick.com/articles/12285/the-finest-web-site-design-trends-you-may-expect-in-2017).

### Example

HttpCookie cookie = new HttpCookie ("user", "abc"); // create cookie with the name \
and value

cookie.Expires = DateTime.Now.AddHours (2); // specifies cookie expires after \
two hours

Response.Cookies.Add (cookie); // specifies cookie and its value \
is added

HttpCookie cook = Request.Cookies ["user"]; // retrieving cookie information

string val = cook.Value.ToString (); // [store the value](https://answers.mindstick.com/qa/35621/how-would-you-store-the-value-of-a-color-in-a-database-as-efficiently-as-possible) of cookie

**d) Query Strings:** Query strings store values in the URL that are visible to the user. Use query strings when you want a user to be able to e-mail or instant message state data with a URL.

### Example:

Response.Redirect ("Home.aspx?data=user&id=abc"); //sending two values through \
url in the [field data](https://www.mindstick.com/forum/161447/what-s-the-difference-between-lab-data-and-field-data-in-performance-testing) and id \
respectively.

string strData = Request.QueryString ["data"]; //get value from [data field](https://www.mindstick.com/forum/160/problem-in-get-and-set-the-data-field)

string strId = Request.QueryString ["id"]; //get value from id field

## Server – Side State Management

**a) [Application State](https://www.mindstick.com/forum/160039/what-is-redux-and-how-does-it-help-manage-application-state-in-react):** Application state is stored in memory on the server and is faster than storing and retrieving information in a database. Application state is a useful place to store small amounts of often-used data that does not change from one user to another.

**Example:**\
Application["noVisitors"] = 1; // set value in the field name noVisitors

int val = (int)Application["noVisitors"]; //get value from application field

**b) [Session State](https://www.mindstick.com/articles/42/session-state-in-asp-dot-net):** ASP.NET Session state provides a place to store values that will persist across page requests. Values stored in Session are stored on the server and will remain in memory until they are explicitly removed or until the Session expires.

### Example:

Session["emailid"] = "abc@gmail.com"; // set value in the session field

string email = Session["emailid"].ToString(); // get value from the session field

---

Original Source: https://www.mindstick.com/blog/219/state-management-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
