title: Creating a back button in ASP.NET
body: <p>I have an old application which has numerous webforms. On every <code>aspx</code> page there is a designated back button, which takes the user to the previous page. All the buttons have the javascript <code>onclick</code> event set to <code>history.back()</code>.  </p>
  
  <p>On one of the <code>aspx</code> pages, the back button would not work at all. We decided to use a server side button that would accomplish the same thing. The onclick event of the button has the necessary code to take the user to previous page.</p>
  
  <p>First I created the onclick event in the following way, but the back button did not work at all. When I went in the debug mode, I found that the <code>prevPage</code> variable was holding the current page URL instead of the previous page URL.</p>
  
  <pre><code>private void btnBack_Click(object sender, System.EventArgs e)
  {
      string prevPage = Request.UrlReferrer.ToString();
      Response.Redirect(prevPage);
  }
  </code></pre>
  
  <p>Then I changed the <code>prevPage</code> to a static variable and defined its value to be set inside the <code>if(!IsPostBack)</code> block. Now the back button worked correctly. </p>
  
  <pre><code>static string prevPage = string.Empty;
  private void Page_Load(object sender, System.EventArgs e)
  {
      if(!IsPostBack)
      {
          prevPage = Request.UrlReferrer.ToString();
      }
  }
  
  private void btnBack_Click()
  {
      Response.Redirect(prevPage);
  }
  </code></pre>
  
  <p>I did not understand why the first attempt wouldn't work and why second attempt would. </p>
  
  <p>I have a feeling that this might explain if there is something else going on with this particular page that would also not let the <code>history.back()</code> or <code>history.go(-1)</code> work. </p>
  
  <p>Can you please provide some insight into this? </p>
  
  <p>Edit: I am using IE8.</p>