代码之家  ›  专栏  ›  技术社区  ›  drovani

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

  •  0
  • drovani  · 技术社区  · 15 年前

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

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

    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?
        }
    }
    
    1 回复  |  直到 15 年前
        1
  •  0
  •   Wizzard    15 年前

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

    推荐文章