× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to work with Client-Side State Management in ASP.NET

In this article we will have a look at some of the client-side state management techniques in ASP.NET.

Web pages almost always need to track users who visit your web pages – to provide personalisation, track usage or store information about users. This is done using state management. State management is the process by which ASP.Net manages state and page information over multiple pages for single or multiple web pages. If your application needs to scale to thousands of users then you must consider the various state management techniques made available by ASP.Net. This reduces load on your servers thereby enabling it to process more user requests. ASP.Net provides two types of state management techniques.

  • Client-side
  • Server-side

In this article we will have a look at some of the client-side state management techniques.

The client-side state management techniques available are:

  • View state
  • Control state
  • Hidden fields
  • Cookies
  • Query strings

Let us have a look at each of the above

View state

View state is a mechanism by which ASP.Net stores user-specific request and response data between requests. View state is not stored on the server, but is stored in the page itself. When the page is submitted to the server the view state is also submitted to the server. The web server pulls the view state from the page to reset the property values and controls when the page is being processed. This allows ASP.Net to have all the object data available to the web server without having to store it on the server. This results in a scalable web server which can handle more user requests.

Imagine a scenario where a user can edit some profile information. When processing the request the page might have to get information from the database. In this case the data in various controls and the property values are stored in the view state. Suppose there is a problem with the submitted data and the page is returned back to the browser. You can see that the controls are populated with the data you entered and the property values are preserved. This is because the data is wrapped up into view state with your request and ASP.Net client-side state management features takes care of all this work for you.

Unless disabled, view state is a part of every ASP.Net page. The view state values are hashed, compressed and encoded for Unicode implementations which provide better optimization and security than simple hidden fields.

Listing 1: Example of view state

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTY1NDU2MTA1MmRkqIBFV24wUH0Ny5RdjT5FltHvW5EIx6oxq6sSsk4aVOk=" />

View state is available as a hidden field as shown above. You can view source of any .Net page and you will come across a hidden field as above.

You can read and write View state collection to store any custom information you have

Listing 2: Writing data to view state

this.ViewState.Add(“customdata”,”I am storing some data in view state”);

Listing 3: Reading view state data

string customdata=(string)ViewState[“customdata”];

Data is stored as objects in View state, so you should cast it to the correct data type before using it. You can store a wide variety of data in view state. Any object that is marked serializable can be added to view state.

Adding data to view state is great when you want to preserve information with page postback. However it does not transfer information from one page to another.

Control state

Control state(ControlState property) is used to save state information for custom controls that require view state. It helps to persist state information for custom control but cannot be turned off like ViewState property.

Hidden fields

ASP.Net provides a HiddenField control to store data on a page. It can be added on to a page like any other .Net control but is not visible on the page unless someone views the page’s source. It stores the data in its Value property.

Like view state it can be use to store information on a single page and it cannot transfer data between pages. But the data stored in the HiddenField is not hashed or encrypted. So it cannot be used to store sensitive information.

To use hidden fields you must use HTTP POST to submit your request to the server. You cannot use HiddenField with HTTP GET.


Web applications often need to track users between page requests. These applications need to ensure that the user making the first request is the same as the user making subsequent requests and this is done using a state management technique called cookies. A cookie is a small amount of data that you write to the client to be stored and passed with every subsequent request to the server. You can even have persistent cookies which saves a small file on the client machine. This means that you can not only pass information between pages but also shut down and return to see that you still have your information intact.

Cookies are the most flexible way of storing information on the client. However the users can delete the browser cookies thus getting rid of the data.

You can write data into a cookie collection and read data from cookie. To create a cookie you add it to the HttpCookieCollection using the Response.Cookies.Add method

Listing 4: Write into a cookie collection

Response.Cookies.Add(new HttpCookie(“cookiedata”, “This is some data stored in the cookie”));

Data is always stored as name-value pairs

To read a cookie you use the Request.Cookies method. You must always ensure that the cookie is not null before you use it

Listing 5: Reading cookie data

String readdata;
If(Request.Cookie[“cookiedata”] != null)

You can also set an expiration for the cookie using the Expires property.

Listing 6: Setting cookie expiry


Here the cookie is allowed to expire after a day.

You can also control the scope of your cookie so that applications or pages outside the scope do not have access to your cookie.

Listing 7: Setting cookie scope


Here the scope of the cookie is set to a “MyApp” virtual directory. Pages outside of this cannot access the cookie.

You can also to set the cookie scope to a domain.

Listing 8: Setting a domain as cookie scope


In this case the cookie is available to all pages in contoso.com, www.contoso.com and any of its subdomains.

You can also store multiple values in a cookie. Multiple values are stored as multiple key-values in a single cookie.

Listing 9: Cookies with multiple values


This code sends a single cookie to the server, but it has multiple values. The properties such as Expires, Domain needs to be set at cookie level. It cannot be controlled at key level.

The size of your cookie depends on your browser. Each of your cookie can be up to 4KB in size. The number of cookies per site is limited to 20.

Query strings

Query strings are appended to the page url and is used to store values for the requested page. They appear in the url after a questionamrk “?” followed by the name of the query string parameter, an equal sign “=” and finally the value.

Listing 10: Example of url with a single query string parameter


In this above listing pageid is the query string parameter name and 10 is the value. You can have any string value as the parameter name.You can also have multiple query string parameters in the same url where each parameter-value pair is separated by “&”.

Listing 11: Example of a url with multiple query string parameters


In the above listing there are two query string parameters – pageid and region.

Query string helps you transfer information between pages . You can have as many query string parameters as you want in your url, but browsers usually limit the have a character limit of 2083 for urls. Another limitation is that the query string parameters are available only on HTTP GET. Another drawback is that users can modify query string parameters to get access to information. But if you want to email a page to your fiend or bookmark specific information this is the best available option.

To add a query string parameter to any hyperlink, you must add the parameter name and the value and append it to the value of NavigateUrl property. So if the NavigateUrl property reads http://www.page.aspx, you can append “?pageid=10” so that the url reads as http://www.contoso.com/page.aspx?pageid=10 . You can also add multiple query strings in the same manner.

To read query string values you access the Request.QueryString collection. You should always encode the value read to avoid trouble caused by any rogue user trying to pass some HTML code into your page.

Listing 12: Reading query string parameters

string region;
region= Server.HtmlEncode(Request.QueryString[“pageid”]);

I hope this article was helpful. See you next time with some more information on state management in ASP.Net

I am a Senior Web Developer from London with over 5years of experience in ASP.NET, HTML, CSS. I am well versed in VB.Net, C# and SQL as well.

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login