在2.0中使用Try Catch Block故障的GridView SelectedIndexChanged?

时间:2009-12-14 16:37:41

标签: c# asp.net vb.net gridview

我正在使用ASP.NET 2.0。

当我在我的事件中放置一个TRY CATCH块时,它总是进入CATCH部分,在我的情况下它将页面重定向到Default.aspx。但是如果我删除TRY CATCH块,代码就会被执行得很好并且它会按照它的设想执行。

Protected Sub gridResults1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridResults1.SelectedIndexChanged

    Try

        Dim selectedRowIndex As Integer
        selectedRowIndex = gridResults1.SelectedIndex

        Dim row As GridViewRow = gridResults1.Rows(selectedRowIndex)
        Dim theCompanyProfile As String = gridResults1.DataKeys(selectedRowIndex).Value

        Response.Redirect("Report.aspx?ID=" + theCompanyProfile)

    Catch ex As Exception

        Response.Redirect("Default.aspx")

    End Try

End Sub
  • 当我通过“Catch ex As Exception”
  • 放置断点时没有错误消息
  • 我是否错误地读取了selectedRowIndex DataKey值?

提前致谢!

2 个答案:

答案 0 :(得分:2)

这是因为Response.Redirect方法终止了页面的执行线程并抛出System.Threading.ThreadAbortException

您可以捕获ThreadAbortException并忽略它(我猜不是最佳做法),或者将false传递给endResponse方法的第二个参数(Response.Redirect)。

请查看此页面以获取更多信息:http://support.microsoft.com/kb/312629

答案 1 :(得分:0)

可以肯定的是,您是否在GridView上设置了DataKeyNames属性?