ASP.NET - difference between viewstate and global variable

Asked By sri on 26-Aug-11 01:42 AM
difference between viewstate and global variable
aneesa replied to sri on 26-Aug-11 01:44 AM

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 different round-trips of a single page as viewstate of a page is not accessible by another page.
Because Viewstate render with the page, it consumes bandwidth, so be careful to use it in applications to be run on low bandwidth.

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 retrieve user specific information required on multiple pages.
Session variables consumes server memory, so if your may have a huge amount visitors, 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 for a particular website o publicly accessible
The purpose of cookies is to help 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:

1- Viewstate is bandwidth hungry
2- Session variables are memory hungry as per number of users
3- Applications variables are shared
4- Cache is memory hungry as per number of resources
5- Cookies are the least secure

dipa ahuja replied to sri on 26-Aug-11 01:51 AM
Global variables should always be used with caution. They are the best means of storing data that has to be accessed anywhere. The most common ways of accessing global variables in ASP.NET are by using Application, Cache, and Session objects.

Viewstate provides no security.Viewstate is only accessible within the page that it was created while session can be accessed in your entire application. Also you can set the timeout for your session in your web.config so that the session expires after a certain period of inactivity. And of course, like they said...Viewstate isn't really secured. :D
Cosmos Mysore replied to sri on 26-Aug-11 01:56 AM
Hi,

Viewstate
  is the technique used by an ASP.NET Web page to persist changes to the state of a Web Form across postbacks.
  The view state of a page is, by default, placed in a hidden form field named __VIEWSTATE.
  This hidden form field can easily get very large, on the order of tens of kilobytes
  Not only does the __VIEWSTATE form field cause slower downloads, but, whenever the user posts back the Web page, the contents of this hidden form field must be posted back in the HTTP request, thereby lengthening the request time, as well.

Global Variable
  are the best means to store data that has to be accessed through out the application.
  
   Example: We can store global data in Application 
         Application("UserID") = "[UserID]"; // to store
         String UserID =   Application("UserID").ToString();

  Another way to store golbal data is using static propertuies

   Example: 

    
public static class GlobalVariable
{
    /// <summary>
    /// Global variable storing important stuff.
    /// </summary>
    static string _userName;
    /// <summary>
    /// Get or set the static important data.
    /// </summary>
    public static string UserName
    {
	get
	{
	    return _userName;
	}
	set
	{
	    _userName= value;
	}
    }
}

         
Reena Jain replied to sri on 26-Aug-11 06:20 AM
hi,

Global variables should always be used with caution. They are the best means of storing datahttp://www.dotnetuncle.com/aspnet/72_global_variables.aspx# that has to be accessed anywhere. The most common ways of accessing global variables in ASP.NET are by using Applicahttp://www.dotnetuncle.com/aspnet/72_global_variables.aspx#tion,, Cache, and Session objects.

ViewState is a collection bag which holds key value pairs of changed control attributes
. You can also utilize it to store your own values which will persist though a Page's postbacks. ViewState is a member of Page, which means it is globably accessible throughout your Page. The ViewState is loaded early on in the Page creation (life cycle), and towards the end it is encrypted and outputed into the HTML. Therefore, when you add some data to the ViewState on the first page load, that data is being stored (encrypted) in the __ViewState hidden field you'll find in the HTML source. When the page is loaded again (after you click the button), that value is decrypted and loaded into the ViewState bag, which means you'll be able to access it.

Riley K replied to sri on 27-Aug-11 12:08 PM

Hi,

ViewState is the variable that holds the current state of the page, which is held in a hidden field in the page (used frequently)



ApplicationState is a variable you can store values in during the life off the application (may get cycled periodically, and without your knowledge) (used less-frequently)

Session is the variable you can write to that will persist from the moment they hit your site until they close the browser. (barring any timeouts). (used frequently)


Check this link

http://www.articlesbase.com/programming-articles/how-to-choose-from-viewstate-session-application-cache-and-cookies-443393.html