插入过程中发生SqlException时如何检索DynamicData DetailsView Viewstate?
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我倾向于将错误包装在模型中的验证异常中,这会导致验证错误,并且应该使您的用户输入保持不变。
史蒂夫
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