Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewState Vs Session ... maintaining object through page lifecycle

Can someone please explain the difference between ViewState and Session?

More specifically, I'd like to know the best way to keep an object available (continuously setting members through postbacks) throughout the lifecycle of my page.

I currently use Sessions to do this, but I'm not sure if it's the best way.

For example:

SearchObject searchObject; protected void Page_Load(object sender, EventArgs e) {      if(!IsPostBack)      {          searchObject = new SearchObject();          Session["searchObject"] = searchObject;      }      else      {          searchObject = (SearchObject)Session["searchObject"];      } } 

that allows me to use my searchObject anywhere else on my page but it's kind of cumbersome as I have to reset my session var if I change any properties etc.

I'm thinking there must be a better way to do this so that .NET doesn't re-instantiate the object each time the page loads, but also puts it in the global scope of the Page class?

like image 406
Kyle Avatar asked May 21 '10 14:05

Kyle


People also ask

Which is better session or ViewState?

The basic difference between these two is that the ViewState is to manage state at the client's end, making state management easy for end-user while SessionState manages state at the server's end, making it easy to manage content from this end too. ViewState: It is maintained at only one level that is page-level.

What is the difference between ASP session state and ASP.NET session state?

In Asp, the session is cookie dependent . That is, Asp session only function when browser supports cookies. While Asp.Net supports cookieless session, so the session in Asp.Net is cookie independent .

What type of data is a session state variable designed to store?

Session variables are single-user global data stored on the web server, meaning by default a session state variable is stored in the web server memory and is available across all pages but it will be for a single session. Client Machine Cookies are being used by a session to store a session id.

What is ViewState used for?

View state is used automatically by the ASP.NET page framework to persist information that must be preserved between postbacks. This information includes any non-default values of controls. You can also use view state to store application data that is specific to a page.


1 Answers

If the search object isn't huge in size, then go with using a ViewState. A ViewState is perfect if you only want the object to live for the current page's lifecycle.

A session object is also fine to use, but obviously once the search object is in there, it will be around for longer the page's lifecycle.

Also, one thing I do with ViewState/Session objects is wrap their access with a property:

public object GetObject {     get     {         return ViewState["MyObject"];     }     set     {         ViewState["MyObject"] = value;     } } 

I tend to find it cleaner to do it this way. Just change the above code to fit your needs.

  • Source 1

  • Source 2

like image 195
Jason Evans Avatar answered Sep 28 '22 13:09

Jason Evans