State Management Techniques in ASP.NET: Part 1

Here we will discuss some of the commonly used state management techniques in ASP.NET.

Web applications are stateless by nature which means that when we enter some information on the web page and post the page, the information will be lost when it reaches the server since client and server are two different entities and are at different locations. So suppose you enter a value in the text field and post it to the server, by default you won't see the value in the text field when you get the response page back from the server. To overcome this limitation of the web applications we have a few techniques known as state management to preserve the page and control values between the round trips.

We can divide these state management techniques into the following two categories.

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

Commonly used client-side state management techniques in ASP.Net are.

  • View state
  • Hidden fields
  • Cookies
  • Query strings

Commonly used server-side state management techniques in ASP.Net are.

  • Application state
  • Session state
  • Profile Properties

View State

View state stores page-specific information. The page uses this method by default to preserve the page and control values between the round trips. The page saves the current state of the page and controls it as a hashed string in a hidden field on the page. We can also store values in the view state to preserve page-specific values between round trips. The advantage of view state is that we can use it to store either page or control values or any other custom value. The disadvantage is that view state value is not encrypted, it is just hashed so it can be easily manipulated by the client. Since the view state value is hashed it appears that it is encrypted but it is just a hashed string.

Another disadvantage of view state is that it increases the page size. If you have a control like a data grid, it can increase the page size considerably. So if you are not using the view state for data controls like the data grid then it is always better to disable the view state.

Hidden Fields

We can store information in a hidden field. It's like a normal ASP.Net control but is not rendered in the browser. Like a standard ASP.Net control, we can set its properties but unlike a standard ASP.Net control, it's not rendered in the browser. When submitted to the server the hidden field is sent as a hidden form element to the server. The advantage of the hidden field is that it is lightweight. The disadvantage is that it works only with HTTP-POST, so we cannot use other HTTP methods like GET. Another disadvantage is that the client can manipulate the hidden field.

Cookies

Cookie stores data on the client, either in a text file or in the browser session. We define cookies to be permanent or being stored temporarily. The cookies stored in the browser session are temporary. When adding cookies to the response we can define the expiration date of the cookie. The cookie is deleted automatically after the expiration date.

The advantage of cookies is that since they are stored on the client they don't impact the server memory. The disadvantage is that since a cookie is stored on the client it can be manipulated by the client.

We can set a simple persistent cookie using the following code. The following code sets the cookie to expire after 1 year.

HttpCookie userName = new HttpCookie("userName", "Ashish");
userName.Expires.AddYears(1);
Response.Cookies.Add(userName);

Query Strings

A Query String is data appended to the end of the URL.

A Query String is information that is appended to the end of a page URL. A query string looks like the following example:

www.samplesite.com?name=ashish

The advantage of using cookies is that they are simple to use to store information between the requests. The disadvantage of using cookies is that most browsers have a limit on the length of the URL, it is usually 2083 characters. Also we can pass only simple strings using the Query String. So if you have a complex data type, like an employee object, then you cannot pass it using the Query String.

In the next article, we will see some of the commonly used server-side state management techniques.


Similar Articles