title: ASP.NET: How to persist Page State accross Pages?
body: <p>I need a way to save and load the Page State in a persistent manner (Session). The Project i need this for is an Intranet Web Application which has several Configuration Pages and some of them need a Confirmation if they are about to be saved. The Confirmation Page has to be a seperate Page. The use of JavaScript is not possible due to limitations i am bound to. This is what i could come up with so far:</p>
  
  <p>ConfirmationRequest:</p>
  
  <pre><code>[Serializable]
  public class ConfirmationRequest
  {
      private Uri _url;
      public Uri Url
      { get { return _url; } }
  
      private byte[] _data;
      public byte[] Data
      { get { return _data; } }
  
      public ConfirmationRequest(Uri url, byte[] data)
      {
          _url = url;
          _data = data;
      }
  }
  </code></pre>
  
  <p>ConfirmationResponse:</p>
  
  <pre><code>[Serializable]
  public class ConfirmationResponse
  {
      private ConfirmationRequest _request;
      public ConfirmationRequest Request
      { get { return _request; } }
  
      private ConfirmationResult _result = ConfirmationResult.None;
      public ConfirmationResult Result
      { get { return _result; } }
  
      public ConfirmationResponse(ConfirmationRequest request, ConfirmationResult result)
      {
          _request = request;
          _result = result;
      }
  }
  
  public enum ConfirmationResult { Denied = -1, None = 0, Granted = 1 }
  </code></pre>
  
  <p>Confirmation.aspx:</p>
  
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
      {
          if (Request.UrlReferrer != null)
          {
              string key = "Confirmation:" + Request.UrlReferrer.PathAndQuery;
              if (Session[key] != null)
              {
                  ConfirmationRequest confirmationRequest = Session[key] as ConfirmationRequest;
                  if (confirmationRequest != null)
                  {
                      Session[key] = new ConfirmationResponse(confirmationRequest, ConfirmationResult.Granted);
                      Response.Redirect(confirmationRequest.Url.PathAndQuery, false);
                  }
              }
          }
      }
  </code></pre>
  
  <p>PageToConfirm.aspx:</p>
  
  <pre><code>    private bool _confirmationRequired = false;
  
      protected void btnSave_Click(object sender, EventArgs e)
      {
          _confirmationRequired = true;
          Response.Redirect("Confirmation.aspx", false);
      }
  
      protected override void SavePageStateToPersistenceMedium(object state)
      {
          if (_confirmationRequired)
          {
              using (MemoryStream stream = new MemoryStream())
              {
                  LosFormatter formatter = new LosFormatter();
                  formatter.Serialize(stream, state);
                  stream.Flush();
  
                  Session["Confirmation:" + Request.UrlReferrer.PathAndQuery] = new ConfirmationRequest(Request.UrlReferrer, stream.ToArray());
              }
          }
          base.SavePageStateToPersistenceMedium(state);
      }
  </code></pre>
  
  <p>I can't seem to find a way to load the Page State after being redirected from the Confirmation.aspx to the PageToConfirm.aspx, can anyone help me out on this one?</p>