Asp.net state management technique

Learn diferent Asp.net state management using Session, Cookies, ViewState.

What is State Management?
HTTP (Hyper Text Transfer Protocol) is a stateless protocol; http cannot hold value in between two requests. So there has to be way to hold data in between two requests.

The technique to store the information in between requests and to retrieve them when required , that's called state management.

There are two types of state management techniques in asp.net, client side and server side.

Client side state management
  • ViewState
  • Cookies
  • Hidden Field
  • Control State
Server side state management
  • Application
  • Session

Different state management in Asp.Net

State Management using ViewState

ViewState is a client side state management mechanism provided by Asp.Net Framework page post back. ViewState provides page level state management. by default EnableviewState property in asp.net page is true, if you want to turn off the property for some particular page, you can set EnableviewState=false in page directive.

<%@ Page Language="C#" EnableViewState="false" %>
            
// to store value in ViewState 
ViewState["keyName"] = "Value in ViewState";
           
// to read value in viewstate
var _vsValue = ViewState["keyName"];

If you turn off ViewState property then control won’t be able to hold data during post back, that means on button click when the page make a server trip and re-loaded, control will take value from server side only, whatever value you entered from client side will be lost.

Some useful additional info in this context:
Turning off ViewState in some situation is good idea and necessary, when you want the data on page to be fetched from database only and some authorization to be performed before pulling data, in such situation turning off ViewState will be good idea, this will also help reducing the page size and render little faster than usual.
State Management using Cookies

Cookies can be used for state management. Cookies are a small text file that stores user information. Normally a cookie is used to identify users.
When any user makes a request for any web page for the first time, the server creates a cookie and sends it to the client browser along with the page, then the client browser receives that cookie and stores it on the client machine.

There are two types of Cookie

Persistent Cookies

persistent cookies is a small text file stored in client's hard disk.

Non-persistent Cookies

non-persistent cokies are stored in browser tempararily. its also called in-memory cookies.

Response.Cookies["wtrCookies"].Value = "Cookies Example";
Response.Cookies["wtrCookies"].Expires = DateTime.Now.AddDays(10); 

// or
HttpCookie cObj = new HttpCookie("wtrCookies");
cObj.Value = "Cookies Example";
cObj.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(cObj);

// to read cookies
var _readCookie = Request.Cookies["wtrCookies"];

read how to use Cookies in Asp.net MVC application

Session State Management in Asp.net

Session means visit duration of an user on any website, when any user access any website Session object is created and used for storing user specific data like username, user role etc, so wherever authorization required that can be checked from session object without pulling data from database.

Remember we should not store any type of master data or application data in session, session is very user specific, once user logout, the session is destroyed and all data for that particular session gets clear.

class HttpSessionState
            
//store information in session object
Session["wtrSession"] = "user information";
            
// read from session object
var userInfo = Session["wtrSession"];

You should read how to use Session in Asp.net MVC application

Application State Management in Asp.net

ASP.Net Application State is a server side state management technique.
We use Application state as global storage mechanism, when we store data in Application state that’s become common to all user of that application, Data from Application state can be accessible anywhere in the application.

Application state use System.Web.HttpApplicationState class

Void Application_Start(object sender, EventArgs e)
{
Application["welcomeMessage"] = "Welcome to WebTrainingRoom";
}

Now if you access this Application["welcomeMessage"] variable anywhere, the value will be same for all the users in that application.

Asp.Net C# Examples | Join Asp.Net Course | Learn Asp.net MVC | Asp.net Core Tutorial