954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Differences between Session and ViewStates?

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

Thanks and with regards,
Madhusudhan

cumadhu
Newbie Poster
19 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

"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

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

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.

meeram395
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You