Why Use State Management :

As we know HTTP is a stateless protocol. Every time server serves any request from the user, it clear all the resources used to serve that request.
If we have to find the user’s information between page visits and even on multiple visits of the same page, then we need to use the State management techniques provided by ASP.NET.

index

 

“State management is the process by which ASP.NET let the developers maintain state and page information over multiple request for the same or different pages.”

Types of State Management

There are mainly two types of state management that ASP.NET provides:

1.Client side state management
2.Server side state management

Client side state management:-

In client side state management, the state related information will be stored on client side(on the user’s browser).
The main benefit is it saves a lot of server memory.

eg:- passwords, creditcard number and payable amount on client side, we need server side state management for such things.

Server side state management:-

In Server side state management, It keeps all the information in user memory. The downside of this is more memory usage on server and the benefit is that users’ confidential and sensitive information is secure.

Client side state management techniques

1.View State
2.Control State
3.Hidden fields
4.Cookies
5.Query Strings

Server side state management techniques

1.Application State
2.Session State
3.Database
4.Caching

View State

ASP.NET uses this mechanism to track the values of the controls on the web page between page request for same page. We can also add custom values to view state. ASP.NET framework takes care of storing the information of controls in view state and retrieving it back from viewstate before rendering on postback.

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” ViewStateEncryptionMode=”Always”%>
Example implementation for viewstate:-
We have a simple web page with a textbox and a button. The idea is that we will write something in the text box and see how ASP.NET stores this information in view state. We will store our own information in the view state too. When we run the page and write my name in the textbox and press the button, a postback occurs but my name still remains in the textbox.

protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack == true)
{
if (ViewState[“number”] != null) //Lets retrieve, increase and store again
{
ViewState[“number”] = Convert.ToInt32(ViewState[“number”]) + 1;
}
else //First postback, lets store the info
{
ViewState[“number”] = 1;
}

Label.Text = ViewState[“number”].ToString();
}
}

Control State

We now know what a viewstate is and we also know that we can disable viewstate for controls on the page. But imagine if we are developing a custom control and we internally are using viewstate to store some information but the user of the control can disable the viewstate for our control. To avoid this problem, we can have viewstate like behavior which cannot be disabled by control users and it is called ControlState. Control states lies inside custom controls and work the same as viewstate works.

“To use control state in a custom control, we have to override the OnInit method and call the RegisterRequiresControlState method during initialization. Then we have to override the SaveControlState and LoadControlState methods.”

Hidden Fields

Hidden field are the controls provided by the ASP.NET and they let use store some information in them. The only constraint on hidden filed is that it will keep the information when HTTP post is being done, i.e., button clicks. It will not work with HTTP get. Let us do the same exercise of keeping track of postbacks using HiddenFields now.

Note: ViewState also uses hidden field underneath.

There are scenarios when we need to store the data between page requests. So far, the techniques we have discussed store the data for the single page requests. Now we look at the techniques that store information between page requests.

Cookies

Cookies are small pieces of information that can be stored in a text file on user’s computer. The information can be accessed by the server and can be utilized to store information that is required between page visits and between multiple visits on the same page by the user. Let us do the same exercise of keeping track of postback by using cookies.

int postbacks = 0;
if (Request.Cookies[“number”] != null) //Lets retrieve, increase and store again
{
postbacks = Convert.ToInt32(Request.Cookies[“number”].Value) + 1;
}
else //First postback, lets store the info
{
postbacks = 1;
}
Response.Cookies[“number”].Value = postbacks.ToString();

Label3.Text = Response.Cookies[“number”].Value;

The cookies can have various parameters like how long they are valid and when should they expire. These parameters can be manipulated as:

Response.Cookies[“number”].Expires = DateTime.Now.AddDays(1);

This cookie will expire after 1 day of its creation.

Query Strings

Query strings are commonly used to store variables that identify specific pages, such as search terms or page numbers. A query string is information that is appended to the end of a page URL. They can be used to store/pass information from one page to another to even the same page. Let us work on storing the postback information in querystrings now:

//GetDataItem from querystring
if (Request.QueryString[“number”] != null) //Lets retrieve, increase and store again
{
Label4.Text = Request.QueryString[“number”];
}

//set in query string
int postbacks = 0;

if (Request.QueryString[“number”] != null) //Lets retrieve, increase and store again
{
postbacks = Convert.ToInt32(Request.QueryString[“number”]) + 1;
}
else //First postback, lets store the info
{
postbacks = 1;
}

Response.Redirect(“default.aspx?number=” + postbacks);

One thing to notice here is that we can no way store the postback information in the query string we are dealing with same page. The reason is that the query string creates a new URL each time and it will be a fresh request each time we use query strings. SO we are now essentially tracking number of click here. The idea behind query string is to pass small information to OTHER pages that can be used to populate information on that page.

eg: http://www.codeproject.com/default.aspx?id=2

client side

Application State

ASP.NET allows us to save values using application state. A global storage mechanism that is accessible from all pages in the Web application. Application state is stored in the Application key/value dictionary. This information will also be available to all the users of the website. In case we need user specific information, then we better use sessionstate.

ASP.NET provides three events that enable you to initialize Application variables (free resources when the application shuts down) and respond to Application errors:

Application_Start: Raised when the application starts. This is the perfect place to initialize Application variables.
Application_End: Raised when an application shuts down. Use this to free application resources and perform logging.
Application_Error: Raised when an unhandled error occurs. Use this to perform error logging.

Let us now store the information of postbacks in application state:

void Application_Start(object sender, EventArgs e)
{
Application[“number”] = 0;
}

//In web pages
Application.Lock();
Application[“number”] = Convert.ToInt32(Application[“number”]) + 1;
Application.UnLock();

Label5.Text = Application[“number”].ToString();

When we run the page and hit the button to do a postback, the web will show us the postbacks being done so far which is being stored in ApplicationState. We can use this object to keep track of clicks by all users on the entire website (see code for details).

Session State

Like Application state, this information is also in a global storage that is accessible from all pages in the Web application. Session state is stored in the Sessionkey/value dictionary. This information will be available to the current user only, i.e., current session only.

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Session[“number”] = 0;
}

// Web forms
Session[“number”] = Convert.ToInt32(Session[“number”]) + 1;

Label6.Text = Session[“number”].ToString();

When we run the page and hit the button to do a postback, the web will show us the postbacks being done so far which is being stored in SessionState. We can use this object to keep track of clicks by the current user, i.e., who owns the session for the entire website (see code for details).

Advantages of Client Side State Management

Better scalability
Support for multiple browser

Advantages of Server Side State Management

Better security
Reduced bandwidth

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply