I wrote the following piece of code in a page which is under Update Panel.
protected void myGrid_RowCommand(object sender, GridViewCommandEventArgs e) { try { if (e.CommandName =="EditNames") { long lSelectedName = Convert.ToInt64(e.CommandArgument); Session["SelectedItem"] = lSelectedName; Response.Redirect("EditItem.aspx"); } else if (e.CommandName =="DeleteNames") { long lSelectedName = Convert.ToInt64(e.CommandArgument); ValidName.DeleteItem(lSelectedName); ScriptManager.RegisterStartupScript(this, GetType(), "Key", "alert('Name deleted sucessfully')", true); } } catch (System.Threading.ThreadAbortException) { } catch (Exception ex) { Error handling code... } }
Here, I am getting a Thread Abort Exception while redirecting. However, I resolved it by using an error handler System.Threading.ThreadAbortException
.
But I am unsure why that error came while redirecting. Even though I solved this problem, I would like to know is there any mistake in the way I am coding or is there any way to stop the error firing at all.
Give your inputs...
Note that the page is under AJAX UPDATE PANEL.
When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException on . NET Framework. ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.
ThreadAbortException is a special exception that can be caught by application code, but is re-thrown at the end of the catch block unless ResetAbort is called. ResetAbort cancels the request to abort, and prevents the ThreadAbortException from terminating the thread.
When you use Response. Redirect("Default. aspx",true ) which is by default true then the execution of current page is terminated and code written after the Response. Redirect is not executed instead of executing code written after the Response.
Please read this article - http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx
Instead of ending the request, it is a good practice to bypass the request execution pipeline by calling the Context.ApplicationInstance.CompleteRequest()
.
So your code would look something like this:
Response.Redirect("TargetPage", false); //write redirect Context.ApplicationInstance.CompleteRequest(); // end response
Even though, i solved this problem , i would like to know is there any mistake in the way i am coding
No mistake, you've done well.
This error is expected. It's thrown because the server thread is in fact aborted when redirecting. From the MSDN documentation:
If you specify true for the endResponse parameter, this method calls the End method for the original request, which throws a ThreadAbortException exception when it completes.
and the documentation for the overload you're using:
Redirect calls End which throws a ThreadAbortException exception upon completion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With