获得在使用的Response.Redirect线程中止异常() [英] Getting Thread Abort Exception while using Response.Redirect()

查看:261
本文介绍了获得在使用的Response.Redirect线程中止异常()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个页面是更新面板下写了下面这段code。

 保护无效myGrid_RowCommand(对象发件人,GridViewCommandEventArgs E)
        {
            尝试
            {
                如果(e.CommandName ==EditNames)
                {
                    长lSelectedName = Convert.ToInt64(e.CommandArgument);
                    会话[的SelectedItem] = lSelectedName;
                    的Response.Redirect(EditItem.aspx);
                }
                否则,如果(e.CommandName ==DeleteNames)
                {
                    长lSelectedName = Convert.ToInt64(e.CommandArgument);
                    ValidName.DeleteItem(lSelectedName);

                 ScriptManager.RegisterStartupScript(这一点,的GetType(),键,警报('名称成功地删除'),真正的);
                }
            }
            赶上(System.Threading.ThreadAbortException)
            {

            }
            赶上(例外前)
            {
                错误处理code ...
            }
 

在这里,我得到一个线程中止异常,同时重定向。如何过,我用一个错误处理程序System.threading.ThreadAbortException解决它。

不过,我不确定为什么会出错,而ridirecting来了。虽然,我解决了这个问题,我想知道的是那里的方式,我的编码或任何的错误是没有任何办法阻止错误射击在所有

让您的输入...

需要注意的是,该网页正在AJAX更新面板

解决方案
  

虽然,我解决了这个问题,我想知道的是那里的方式,我的编码有错

没有错,你已经做得很好。

预计此错误。它抛出,因为服务器线程被重定向,而事实上中止。从 MSDN文档

  

如果您指定true为endResponse参数,这个方法调用End方法原始请求,当它完成时会抛出一个ThreadAbortException异常。

和你使用的超载的文档:

  

呼叫重定向结束其完成时抛出一个ThreadAbortException异常。

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 an Thread Abort Exception while redirecting . How ever , i resolved it by using an error handler System.threading.ThreadAbortException.

But, i am unsure why that error came while ridirecting. 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

解决方案

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.

这篇关于获得在使用的Response.Redirect线程中止异常()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆