ASP.NET - Difference between Viewstate and Session

Created by
Thursday, July 21, 2016

Below outlines the main differences you should consider when storing information in either the view state or the session state. 


Viewstate Session
Viewstates are maintained only at the page level Session states are maintained throught the session of an acrtive user. All pages
When a viewstate is created on a single page it is available for that page only. States cannot be spread over other pages. Session state is held throught all the pages of an a web application. For example facebook will remeber your username and display it at the top menu throught your time logged in.
One main reason viewstate is only per page is that it stores information in the client on that page only. Session state information is stored on the server side and can be accessed via call at any time. 
One thing different is that view state information will withhold a postback. If you refersh a regular HTML page you will loose data. With viewstate chaging items on this page will not remove data you may already have saved.  Sesseion state data will stay on the server side till one of two events happens. Either you close the session (logging out/waiting too long) or close your browser.
Viewstate is mainly used to persist instance spcific data. Session state is used to persist user spcific data on the server side.


Imagine yourself in any web application. Take facebook for example. Every page within facebook you navigate through is held because your user details are maintained within a session state. (*Note: Facebook is not build on ASP.NET. Same rules apply to multiple frameworks).

No imagine on another application you are filling out a form, any form. You get to the end and you hit submit. You have however forgot to input into one of the required fields of that form. The page is telling you that you need to go back and enter data into that field before you can fully submit the form. The good news though is that your data is saved in the viewstate and you do not have to enter it all again.



1 2 3 4 5