插入过程中发生SqlException时如何检索DynamicData DetailsView Viewstate?

发布于 2024-10-05 05:14:55 字数 682 浏览 0 评论 0原文

使用 DyanamicData,我有一个标准的多对多表,在插入新的唯一行时工作得非常好。两个父表的下拉列表都按其应有的方式显示,并且行被正确插入。

但是,在一张表上,我有一个唯一索引,它会引发一个异常,我可以在 OnInserted 事件处理程序中捕获该异常。然后我可以向用户发回一条友好的消息,告知他们问题。出现此问题的原因是,DetailsView 丢失了视图状态,并且用户为 DropDownLists 选择的值被重置回页面的默认值。

protected void DetailsDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
{
    if (e.Exception != null && !e.ExceptionHandled)
    {
        msgPanel.Text = "Error Occurred"; // or some other friendly message
        e.ExceptionHandled = true;

        // e.Result == null, so I can't rebuild the user's input from here.
        // What can be put where to get the user's input restored?
    }
}

Using DyanamicData, I have a standard Many-to-Many table that is working perfectly fine when inserting a new unique row. The dropdownlists for both Parent tables display as they should, and the row gets inserted correctly.

However, on one table, I have a Unique Index, which throws an exception that I can catch in the OnInserted event handler. I can then post back a nice friendly message to the user, informing them of the problem. The problem occurs because the DetailsView loses the viewstate and the values that the user had selected for the DropDownLists gets reset back to the page's defaults.

protected void DetailsDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
{
    if (e.Exception != null && !e.ExceptionHandled)
    {
        msgPanel.Text = "Error Occurred"; // or some other friendly message
        e.ExceptionHandled = true;

        // e.Result == null, so I can't rebuild the user's input from here.
        // What can be put where to get the user's input restored?
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身情人 2024-10-12 05:15:02

我倾向于将错误包装在模型中的验证异常中,这会导致验证错误,并且应该使您的用户输入保持不变。

史蒂夫

I tend to wrap the error in a Validation exception in the model this results in a Validation Error and should leave your user input intact.

Steve

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文