About Links Archives Search Feed Albums of Note

Introducing ThreadAborts

Interesting bug this afternoon. The click handler for a button on a page redirected to another page at the end of the code that was due to run after postback but actually ran twice with the redirect seemingly doing nothing what so ever.

private void btnSave_Click(object sender, EventArgs e) { … Context.Response.Redirect(redirectUrl, true); }

Eventually it emerged that the redirect wasn’t working because it was being called during a postback. Essentially, two calls to Redirect were being made - one implicitly by the postback and one explicitly by the handler code. The internal call took priority and a ThreadAbortException was being raised but not caught by Response.Redirect(). The solution was to explicitly throw the exception to cause the external redirect to work. Sounds a bit backwards, but it seems to work.

The MS support page on this is here [http://support.microsoft.com/default.aspx?scid=kb;en-us;312629].

private void btnSave_Click(object sender, EventArgs e) { try { … Context.Response.Redirect(redirectUrl, true); } catch (ThreadAbortException) { throw; } }

Posted on February 8, 2005   #Geek Stuff  

← Next post    ·    Previous post →