附加了 LinqDataSource 的 GridView - "Row not found or changed" 异常

GridView with LinqDataSource attached - "Row not found or changed" exception

我有一个附有 LinqDataSource 的 GridView。我想控制 "Row not found or changed" 异常提醒用户他试图更新的记录已被其他人修改。

在 LinqDataSource 的 'OnUpdated' 方法中,我可以通过这样做来处理异常:

protected void LDS_Updated(object sender, LinqDataSourceStatusEventArgs e)
{
    if (e.Exception != null && e.Exception.HResult == -2146233088)
    {
        ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "Row not found or changed", "alert('Row not found or changed');", true);
    }
}

问题是引发了 JavaScript 异常并且没有出现警报,如您在我的 Firebug 控制台中所见:

http://i.stack.imgur.com/18F30.png

如何避免 JavaScript 错误并显示我的 'alert'?提前致谢!

e.ExceptionHandled 设为真。

https://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.linqdatasourcestatuseventargs.exceptionhandled(v=vs.110).aspx

这里你只是在做一些异常情况,但你并没有真正处理它,你没有告诉堆栈你已经处理了这种情况,一切都很好,不用担心它。

顺便说一下,我不会检查异常的 HResult 代码(这看起来很不稳定),但我会检查异常的类型。不过不确定这是否是最好的方法。