ViewState is state that is stored for a particular ASP.NET WebForm. It holds values between postbacks for a page. ViewState will be lost once you visit another page and return. Session state is a per user way of storing information for that user, like UserID, FirstName, LastName, etc. It can be used to store just about anything unique to that user.
Both come with a penalty hit, however. ViewState is encrypted and sent to the browser in a hidden field "__VIEWSTATE", so if you have a lot of controls on a page that store their state in ViewState, this field can be bloated and increase the size of the page download. This can be trimmed by setting EnableViewstate = False for any control or page if you wish.
Session state, has 3 different methods of storage: InProc, StateServer, SQL Server. InProc uses the in process memory currently running the ASP.NET application at the moment. If you make a change to your web.config or upload a new .DLL, all session is lost. StateServer uses a separate Windows Service to store state outside of the ASP.NET application. Therefore, if you update (like before), session is not lost, except for a server reboot (This is what I/my company use). Both of these scenarios can increase the amount of memory that is used, so you must be cautious to what you place in session. Lastly, storing state in SQL Server alleviates the headache of web farms and server restarts because it uses SQL Server to store its state. The only penalty you will see here is the constant flow of data between your webserver & sql server.
I hope this helps!
Matt Hawley, MCAD .NET
http://www.eworldui.net
does anyone know the diffrance between
ViewState["variable"]
and
Session["variable"]
and when to use each of them
?