Using Client Side State Management Techniques like QueryString and Cookies

Part of the You Can Learn ASP.Net and C# series.
By Ken Brown
Editor, YouCanLearnSeries.com
Updated:
February 12, 2005

In ASP.Net there are four ways to manage client side state. What does that mean client side state management? When your user clicks on an URL or button or server side control, the information goes from your page to the server and then back again to the users web browser. How do you remember the information that is currently on the page. These are the techniques to save the information on the client side and not the server side.

Unlike a client server application, there is no automatic storage of information from the previous browser page. Therefore the ASP.Net developer has to take steps to save important information from the postback to the server.

The four methods of client side state management are:

  1. Query String
  2. Cookies
  3. Hidden Fields
  4. View State
This article will discuss the advantages and disadvantages of each method of state management.

Query String

The query string is a holdover from the ASP days of web programming. You will see this alot when you are surfing around the internet. Basically, it is passing information to the next page with the URL. In it's simplest form it is an URL, with a question mark ?, followed by a key value pair.

So let's say you wanted to pass the user's name to the next page requested. Your redirected URL is http:/www.youcanlearnseries.com?name=Joe+Smith. When the user goes to the next page, the developer only needs to capture the query string in the page postback and you have successfully "saved" information from the previous page.

To read the query string you use the HttpRequest object.

string sName = Request.QueryString["name"];
To pass two or more key value pairs through the querystring property use an ampersand between keyvalue pairs. It would look like this:

myURL.com?name=joe+smith&state=Illinois
To pass a value to the querystring use:
Request.QueryString["state"] = sState.ToString();
One of the advantages to using querystring is it requires no postback operation from the server. The limitations and disadvantages include:
  1. There is a limit to the number of characters browsers will allow the length of the querystring to be.
  2. There is virtually no security with querystring. Any information you send in a querystring can be read by anybody else.
  3. There is no persistence. There is no way to remember the information in a querystring after the user leaves the page.
  4. You are limited to using only strings. There is no support for storing structured data, such as ArrayLists, controls, structures or classes.
There is still a lot of uses of querystring, because it is simple to read and send the key value pair between pages, you just have to careful what you send and you have to know the limitations of the querystring.

Cookies

Next is the ASP classic cookies. You are aware of cookies. When you go to a website and the website leaves a txt file on your computer that contains information for the website to use the next time you come to the site.

The Advantages of cookies include the fact that the information can be persisted on the user's computer. You can set the cookies expire property to automatically expire the cookie after a certain time.

The disadvantages of Cookies
  1. Users can disable cookies in their browsers
  2. Size restriction by browser around 4kb to 8kb
  3. Cannot store structured data in cookies
  4. can't leave sensitive information in cookies
Since the cookies reside on the client's computer, any web browser can read the cookies you leave on others' computers. So it is critical you don't leave mission critical information where any smart guy with a browser can pick up your cookies and cause trouble.

The cookies property comes from the HttpReponse object.

Here is the C# code to set a cookie.
//Create a cookie
HttpCookie cCookie = new HttpCookie("UserName");
cCookie.Value = txtUserName.Text;

//To set expiration time of cookie to 30 minutes from now.
cCookie.Expires = DateTime.Now + new TimeSpan(0,0,30,0);

//Add the cookie to users computer
Response.Cookies.Add(cCookie);
You have just added the cookie to the user's computer. When you need to get that information you do the following.

//First check to see if the cookie is available anymore.  Many power users delete 
//their cookies on a regular basis to improve performance.
if (Request.Cookies["UserName"] == null)
{
	//The cookie is not available go on without it.
}
else
{
	//Cookie is still there let's read it.
	string sUserName = Request.Cookies["UserName"].Value;
}
Cookies have their place in the internet world. I go to a site on a regular basis. They place a cookie on my computer and know who I am when I arrive. It saves me the time of having to login when I want to comment in their forum.

Using Client side state management techniques like hidden fields and viewstate.

Home| About Us | NewsLetters | Contact Us |

Copyright © 2004-2014 You Can Learn Series

You Can Learn Series Home Page You Can Learn Series
You Can Learn Series Home Page Home            About the creators of YouCanLearnSeriesAbout Us      Contact YouCanLearnSeriesContact Us      Site map of YouCanLearnSeriesSite Map      Privacy Policy of You Can Learn SeriesPrivacy Policy       Terms of Use of You Can Learn Series web siteTerms of Use       YouCanLearnSeries RSS feed for C# tips and tutorials     
C# and ASP.Net Programming TipsC# and ASP.Net      SQL Tutorial, tips and tricks for DBA'sSQL       Landscaping Tips for Home GardenersLandscaping       WeightLifting Tips for the Young and OldWeightLifting       Kennos BlogBlog       Good health TipsGoodHealth       Learn about Great Travel DestinationsTravel     Web Business TipsWeb Business            You Can Learn Series' Online StoreStore



You Can Learn C# and ASP.Net SeriesC# and ASP.Net
  Using Client Side Code with C# and ASP.NetClient Side Code
  Using Client Side State Management with C# and ASP.NetClient Side State Management
  Create a C# Class in Visual StudioCreate A Class
  Use A Class in C#Use A Class
  Create a Solution in Visual StudioCreate a Solution
  Create a Project in Visual StudioCreate a Project
  Create an RSS feed for your Web SiteCreate an RSS Feed
  Create an RSS feed for your Web SiteListItemCollection
  Set Events in ASP.NetSet Events ASP.Net
  Set Events in C#Set Events C#
  What is Session State and How to Use it.Session State
  Creating and Using the C# SortedList ObjectSortedList Object
  Stylesheet Tips for Visual Studio .Net and ASP.NetStyleSheet Tricks
  Using C# MethodsUsing Methods
  Pass Objects to MethodsPass Objects to Methods
  Pass Objects from MethodsPass Objects from Methods
  Using Client side State management like ViewState and Hidden fieldsView State
  LDAP, What is Lightweight Directory Access ProtocolWhat is LDAP
  Build an LDAP ReaderBuild an LDAP Reader
  Convert information from LDAP to a Web ServiceLDAP To Web Service
  Question of the dayQuestion of the Day

You Can Learn SQL SeriesSQL
You Can Learn Landscaping and Gardening SeriesLandscape Tips
You Can Learn WeightLifting SeriesWeightLift Tips
Kenno's BlogBlog
You Can Learn Good Health SeriesGood Health Tips
You Can Learn Good Series NewslettersNewsletters
Learn about Great Travel DestinationsTravel
You Can Learn Series Web Business TipsWeb Business Tips
Merchandise Available from You Can Learn SeriesMerchandise

Camping and Hiking supplies


Americas Public Schools

Learn insider tips from one of the web’s biggest podcasters Pat Flynn!

Tap Influence Co-Founder Holly Hamann details how to make your blog more brand friendly.

Tiny Cupcake Toppers, with Sheryl Bito

Figure Anatomy for the Artist, with Roberto Osti

BODYBUILDING PROTEIN

Shop Seattle Seahawks Super Bowl Championship Gear at Fanatics.com

Dog Supplies

Lowest Prices on Fruit Trees, Shade Trees, Flower Bulbs, Perennials, House plants, Vegetable Plants, Seed and More!