Thursday, May 8, 2014

ASP.NET Session State Vs ViewState

Session State
  1. Across Multiple Page.
  2. Saved in a specific session, cleared when the session dies (usually after 20 minutes of inactivity).
  3. Saved in Server.
  4. valid for any type of objects.
public List<Contact> SessionContacts
{
    get { return (List<Contact>)HttpContext.Current.Session["SessionConacts"]; }
    set { HttpContext.Current.Session["SessionConacts"] = value; }

}

View State

  1. Only that web page.
  2. It is stored in a hidden field.
  3. Saved in page, travels up and down between client and server.
  4. No expiration date.
  5. Valid for Serializable data only.

public List<Contact> SessionContacts
{
   get { return (List<Contact>)ViewState["VSConacts"]; }
   set { ViewState["VSConacts"] = value; }

}

Conclusion
If data you store in the view state is not large enough to cause slow performance , use view state.
If you store too much data in session and you create many sessions in a short period with many of your concurrent users, you would be killing your server.

No comments:

Post a Comment