title: ViewState Vs Session ... maintaining object through page lifecycle
body: <p>Can someone please explain the difference between ViewState and Session?</p>
  
  <p>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.</p>
  
  <p>I currently use Sessions to do this, but I'm not sure if it's the best way.</p>
  
  <p>For example:</p>
  
  <pre><code>SearchObject searchObject;
  protected void Page_Load(object sender, EventArgs e)
  {
       if(!IsPostBack)
       {
           searchObject = new SearchObject();
           Session["searchObject"] = searchObject;
       }
       else
       {
           searchObject = (SearchObject)Session["searchObject"];
       }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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?</p>