State Management 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:

 

1. Client Side State Management

 

This stores information on the client's computer by embedding the information into a Web page, a uniform resource locator(url), or a cookie. The techniques available to store the state information at the client end are listed down below:

 

View State


Asp.Net uses View State to track the values in the Controls. You can add custom values to the view state. It is used by the Asp.net page framework to automatically save the values of the page and of each control just prior to rendering to the page. When the page is posted, one of the first tasks performed by page processing is to restore view state.

 

Sample Code

 

//To Save Information in View State

ViewState.Add("Cherukuri", "Venkateswarlu");

//Retrieving View state

Label1.Text = (string)ViewState["Cherukuri"];

 

 

Control State


If you create a custom control that requires view state to work properly, you should use control state to ensure other developers don’t break your control by disabling view state.

 

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.

 

Sample Code

 

//Declaring a hidden variable

HtmlInputHidden hidCherukuri = null;

//Populating hidden variable

hidCherukuri.Value = "Venkateswarlu";

//Retrieving value stored in hidden field.

Label1.Text = hidCherukuri.Value;

 

 

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 on a web site.

 

Sample Code

 

//Storing value in cookie

HttpCookie cookie = new HttpCookie("Cherukuri");

cookie.Value = "Venkateswarlu";

Request.Cookies.Add(cookie);

//Retrieving value in cookie

if (Request.Cookies.Count > 0 && Request.Cookies["Cherukuri"] != null)

    Label1.Text = "Welcome" + Request.Cookies["Cherukuri"].ToString();

else

    Label1.Text = "Welcome Guest";

 

 

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.

 

Sample Code

 

//Storing value in query(url)

Response.Redirect("Default.aspx?Cherukuri=Venkateswarlu");

//Retrieving from query string

Label1.Text = Request.Params["Cherukuri"].ToString();

 

 

 

2. Server Side State Management

 

ASP.NET offers you a variety of ways to maintain state information on the server, rather than persisting information on the client.

 

Application State


Application State information is available to all pages, regardless of which user requests a page.

 

Sample Code

 

//Stroing information in application state

lock (this)

{

    Application["Cherukuri"] = "Venkateswarlu";

}

//Retrieving value from application state

lock (this)

{

    string str = Application["Cherukuri"].ToString();

}

 

 

Session State


Session State information is available to all pages opened by a user during a single visit.

 

Both application state and session state information is lost when the application restarts. To persist user data between application restarts, you can store it using profile properties.

 

Sample Code

 

//Storing informaton in session state

Session["Cherukuri"] = "Venkateswarlu";

//Retrieving information from session state

Label1.Text = Session["Cherukuri"].ToString();

 

 

Profile Properties


ASP.NET provides a feature called profile properties, which allows you to store user-specific data. This feature is similar to session state, except that the profile data is not lost when a user's session expires. The profile-properties feature uses an ASP.NET profile, which is stored in a persistent format and associated with an individual user. The ASP.NET profile allows you to easily manage user information without requiring you to create and maintain your own database.

 

 

Advantages

 

 

Advantages of Client Side State Management:

 

1. Better Scalability:


With server-side state management, each client that connects to the Web server consumes memory on the Web server. If a Web site has hundreds or thousands of simultaneous users, the memory consumed by storing state management information can become a limiting factor. Pushing this burden to the clients removes that potential bottleneck.

 

2. Supports multiple Web servers:


With client-side state management, you can distribute incoming requests across multiple Web servers with no changes to your application because the client provides all the information the Web server needs to process the request. With server-side state management, if a client switches servers in the middle of the session, the new server does not necessarily have access to the client’s state information. You can use multiple servers with server-side state management, but you need either intelligent load-balancing (to always forward requests from a client to the same server) or centralized state management (where state is stored in a central database that all Web servers access).

 

Advantages of Server Side State Management:

 

1. Better security:


Client-side state management information can be captured (either in transit or while it is stored on the client) or maliciously modified. Therefore, you should never use client-side state management to store confidential information, such as a password, authorization level, or authentication status.

 

2. Reduced bandwidth:


If you store large amounts of state management information, sending that information back and forth to the client can increase bandwidth utilization and page load times, potentially increasing your costs and reducing scalability. The increased bandwidth usage affects mobile clients most of all, because they often have very slow connections. Instead, you should store large amounts of state management data (say, more than 1 KB) on the server.