title: How to store ASP.NET WebForms page state between requests
body: <p>First, this is what I want to achieve:</p>
  
  <p>Imagine an ASP.NET page with a GridView and some other controle. The user applies some paging, sorting and filtering to the GridView, then clicks a link and goes to another page. He or she can eventually wander through some more pages, then come back to the GridView page. Then she sees the Grid with the same paging/sorting/filtering when she left the page.</p>
  
  <p>I could do this easily to one page, but I need to apply this behavior to dozens of pages.</p>
  
  <p>I want something to the effect of:</p>
  
  <pre><code>String PageStateKey {
    get { return "PageState_" + Page.UniqueID; }
  }
  
  void Page_PreRender() {
      // this is executed in every postback, and saves page state for future requests
      var pageState = GetCurrentPageState(); // gets an object with ViewState, query string, and form values
      Session[PageStateKey] = pageState; // persist page state to session
  }
  
  void Page_Init() {
      // this restores a page state saved in a previous request, if any
      var restoredPageState = Session[PageStateKey];
      if (restoredPageState != null) {
         ApplySavedStateToPage(restoredPageState);
      }
  }
  </code></pre>
  
  <p>Then my pages could inherit from a <code>StatePage</code> or something like this. I found some solutions but they require changing my code (i.e: using a custom method to redirect pages instead of Response.Redirect, etc.)</p>
  
  <p>Thanks in advance</p>