Friday, December 14, 2007

Better way to handle thread abort exception in Response.Redirect

private bool m_bIsTerminating = false;

protected void Page_Load(object sender, EventArgs e)
{
if (WeNeedToRedirect == true)
{
Response.Redirect(url, false);
HttpContext.Current.ApplicationInstance.CompleteRequest();
m_bIsTerminating = true;
// remember to end the method here if
// there is more code in it

return;
}
}

protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
{
if (m_bIsTerminating == false)
base.RaisePostBackEvent(sourceControl, eventArgument);
}

protected override void Render(HtmlTextWriter writer)
{
if (m_bIsTerminating == false)
base.Render(writer);
}

No comments: