title: Response.Redirect(URL, false) - post-redirect event management
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2777105/response-redirect-causes-system-threading-threadabortexception">Response.Redirect causes System.Threading.ThreadAbortException</a>  </p>
  </blockquote>
  
  
  
  <p>ASP/C#.NET (web forms, not MVC)</p>
  
  <p>UPDATE: Just found a related post (likely rendering this a duplicate): <a href="https://stackoverflow.com/questions/2777105/response-redirect-causes-system-threading-threadabortexception">Why Response.Redirect causes System.Threading.ThreadAbortException?</a></p>
  
  <p>~ ~ ~</p>
  
  <p>After a good bit of research, I have come to the understanding that, in general, when using Response.Redirect(), it is better to pass FALSE for the second parameter, to avoid  System.Threading.ThreadAbortException.  (<a href="http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx</a>)</p>
  
  <p>My question is, "Is there a recommended way (pattern) for managing (namely skipping) processing in page events that fire after the redirect, when false is passed for the second parameter?"  </p>
  
  <p>This is mostly a problem for me when I am checking for and redirecting on an expired session in Page_Load().  It seems very tedious to have to perhaps set a "_Redirected" flag every time I redirect and then check that flag at the top of every event.  I haven't had to worry about this in the past because I always passed TRUE for the second parameter, not knowing any better.</p>
  
  <p>Below is some code showing what I don't want to have to do (check _Redirected before processing each event).  Perhaps what I'm looking for is a better Session Expiration Processing pattern.  </p>
  
  <p>Any suggestions on how to improve this processing, would be greatly appreciated.</p>
  
  <pre><code>private bool _Redirected = false;    
  
  protected void Page_Load(object sender, EventArgs e)
  {
    if (Session["key"] == null)
    {
      Response.Redirect("SessionExpired.aspx", false);
      Context.ApplicationInstance.CompleteRequest();
  
      _Redirected = true;
    }       
  }
  
  protected void Page_PreRender(object sender, EventArgs e)
  {
    if (!_Redirected)
    {
      // do Page_PreRender() stuff...
    }
  }
  
  protected void Button1_Click(object sender, EventArgs e)
  {
    if (!_Redirected)
    {
      // do Button1_Click() stuff...
  
      Response.Redirect("Button1Page.aspx", false);
      Context.ApplicationInstance.CompleteRequest();
  
      _Redirected = true;
    }
  }
  
  protected void Button2_Click(object sender, EventArgs e)
  {
    if (!_Redirected)
    {
      // do Button2_Click() stuff...
  
      Response.Redirect("Button2Page.aspx", false);
      Context.ApplicationInstance.CompleteRequest();
  
      _Redirected = true;
    }
  }
  </code></pre>
  
  <p>~ ~ ~</p>
  
  <p>[01/24/2013] In response to <a href="https://stackoverflow.com/users/2424/chris-lively">https://stackoverflow.com/users/2424/chris-lively</a> (thank you, btw), here is simplified code that I believe is similar to what you tested.  I am still seeing Button1_Click() execute on post back after the Response.Redirect(url, false) with .CompleteRequest() in Page_Load().</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
    if (this.IsPostBack)
    {
      Response.Redirect("Redirect.aspx", false);
      Context.ApplicationInstance.CompleteRequest();
    }
  }
  
  protected void Button1_Click(object sender, EventArgs e)
  {
    Response.Write("Button1 clicked!");
  }
  </code></pre>
  
  <p>This behavior is corroborated by this response <a href="https://stackoverflow.com/a/12957854/1530187">https://stackoverflow.com/a/12957854/1530187</a> to the similar post I noted above in my update.</p>
  
  <p>Any idea what I'm doing wrong that would cause the page to continue executing after redirect? </p>