Hi All,
I would like to know the differences between Sessions and Viewstates. And best uses of Sessions and Viewstates.

Thanks and with regards,

11 Years
Discussion Span
Last Post by Patel Roopal

"Session" is data stored per user session. "Viewstate" is an ASP.NET thing. It's a way for the application to maintain state about a particular form and its child controls. Your ASP.NET application might consist of several pages/forms. There might be a piece of data that you want to access across all pages. You could place that in a Session variable. However, you rarely need to worry about ViewState, ASP.NET manages that itself.

I've written some articles that discuss Viewstate: http://www.tgreer.com/aspnet_html_02.html

This article shows how you might use Session: http://www.tgreer.com/aspnet_html_04.html


Both Session and ViewState are a way of storing data. The following are the differences.
.1) Session is for per user session. ViewState is for a page. ie. Storing data in a session variable will last till the session ends. Whereas storing data in ViewState will last only till the page is in live.
2) Session is server type storage where as ViewState is client side type storage.
3) Session variables are stored in a SessionStateItemCollection object that is exposed through the HttpContext.Session property. ViewState variables are stored in hidden fields.
4) When using a SessionState Mode other than Inproc, the session variable type must be either a primitive type or serializable. This is because the session-variable is stored in an external data store. There is no mode for ViewState. By default, serialization of data will be taken care by the viewstate.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.