Sep
27

How to Choose From Viewstate, Session, Application, Cache, and Cookies

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 load all values and create the page again. ASP.NET provides multiple simple solutions to this problems like: ViewstateSession VariablesApplication VariablesCacheCookies 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 lo breathslim w 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.

Comments are closed.