Click here to Skip to main content
Licence CPOL
First Posted 4 Mar 2008
Views 4,915
Bookmarked 0 times

How to choose from Viewstate, Session, Application, Cache, and Cookies

By | 20 Mar 2010 | Article
This article gives you a breif description and comperison on different state options availabe in ASP.NET

Problem with Web Applications

Web Applications are natively statesless, means once a web page renders from server to client, nothing remains on server and the next time user submits the page you have to create the page again.

ASP.NET provides multiple simple solutions to this problems like:

  • Viewstate

  • Session Variables

  • Application Variables

  • Cache

  • Cookies

Now the question arises that when to use what?

1- Viewstate

Viewstate is a hidden fields in an ASP.NET page, contains state of those controls on a page whose "EnableViewstate" property is "true".

You can also explicitly add values in it, on an ASP.NET page like:

Viewstate.Add( "TotalStudents", "87" );

Viewstate should be used when you want to save a value between diferent roundtrips of a single page as viewstate of a page is not accessible by another page.

Because Viewstate renders with the page, it consumes bandwith, so be careful to use it in applications to be run on low bandwith.

2- Session Variable

Session variables are usually the most commonly used.

When a user visits a site, it's sessions starts and when the user become idle or leave the site, the session ends.

Session variables should be used to save and retrive user specefic information required on multiple pages.

Session variables consumes server memory, so if your may have a huge amount visiters, use session very carefully and instead of put large values in it try to put IDs and references

3- Application variables

Application variables are shared variables among all users of a web application

Application variables behave like static variables and they are substitute of static variables as static variables are stateless in web applications

Only shared values should be persisted in Application variables, and as soon as they are not in use they should be removed explicitly.

4- Cache

Cache is probably the least used state feature of ASP.NET.

Cache is basically a resource specific state persistence feature, means unlike session it stick with resource instead of user, for instance: pages, controls etc.

Cache should be used or frequently used pages, controls, and data structures

Data cache can be used to cache frequently used list of values e.g. list of products

6- Cookies

Cookies are some values saved in browsers by the website to retrivbbe and use afterwards.

Usually cookies are used to help dynamic websites to identify visitors and retrieve their saved preferences.

Cookies are also used to facilitate auto login by persisting user id in a cookie save in user's browser.

Because cookies have been saved at client side, they do not create performance issues but may create security issues as they can be hacked from browser.

Finally remember the following points on your finger-tips:

  • Viewstate is bandwidth hungry

  • Session variables are memory hungry as per number of users

  • Applications variables are shared

  • Cache is memory hungry as per number of resources

  • Cookies are the least secure

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ahmed Siddiqui

Architect
Avanza Solutions
Pakistan Pakistan

Member

Seasoned Microsoft Certified Professional having more than seven years of experience in analysis, design, and development of enterprise applications; utilizing true world class software development patterns & practices.
 
Developed an application framework for writing performant, maintainable and configurable applications on .Net. Currently playing role of Team Lead & Architect on an enterprise software project.

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General.net 2.0 vs 1.0 Pinmemberarshya20:23 12 Jun '08  
AnswerRe: .net 2.0 vs 1.0 PinmemberAhmed Siddiqui22:55 16 Jun '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 20 Mar 2010
Article Copyright 2008 by Ahmed Siddiqui
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid