本文介绍了用于ASP.net&的ErrorProvider(来自Windows窗体) linq到sql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何通知用户哪个字段验证失败.我将LINQ设置为SQL类datacontext,以从ASP.net页面访问数据库.由于用户输入将通过Web界面表单输入并从Excel文件导入,因此我想将验证逻辑写在一个地方.这背后的想法是当我从excel导入时.我将收集每一行的错误消息,并以某种方式显示摘要.在逻辑上,似乎可以将LINQ生成的类扩展到SQL.根据大多数文档和示例,我应该执行以下操作:

I am trying to figure out how to notify the user which field failed to validate.I setup LINQ to SQL class datacontext to access a database from ASP.net pages. Since user input will be from by web interface forms and import from Excel files, i would like the write the validation logic in one place. The idea behind this is when i import from excel. I will collect the error messages for each row, and display a summary, somehow. A logical place seems to extend the class generated by LINQ to SQL. According to most documentation and examples, i should do something like this:

public partial class Customer 
{
  partial void OnTitleChanging(string value) 
  {
    if (!Char.IsUpper(value[0])) {
      throw new ValidationException(
       "Title must start with an uppercase letter.");}
  }
}

此方法的问题在于,验证将在第一个失败的字段上停止.

The problem with this approach is that validation will stop on the first failed field.

在Windows窗体中 Link1 ,如果我在表单中定义一个ErrorProvider组件并将其DataSource属性设置为您的BindingSource,则将在已验证控件的右侧用红色圆圈表示异常.红色圆圈的工具提示将显示异常消息.

In Windows Forms Link1, if I define an ErrorProvider component in the form and set the DataSource property of it to your BindingSource the exception will be indicated by a red circle right to the validated control. The tooltip of this red circle will show the exception message.

ASP.net页面是否有类似内容?我在列表视图中使用列表视图控件和内联编辑.

Is there something similar for ASP.net pages? I am using the listview control and inline editing in the listview.

更新: -实际上,我所做的事情与尼克·卡佛(Nick Carver)的建议相似. Link2 .我记录了一条错误消息,而不是引发异常.

Updates: - I actually did something similar to what Nick Carver is suggesting. Link2 . Instead of throwing an exception, i record an error message.

public partial class PQSSClassesDataContext    
{
public partial class ErrorFeilds
  {
      private static List<string> Messages = new List<string>();
      public void AddErrorMessage(string message)
      {
          Messages.Add(message);
      }
      public List<string> GetErrorMessages() 
      {
          return Messages;
      }
  }
}

实际上,我对如何将错误消息映射到该字段感到困惑.这就是为什么我一直在寻找类似ErrorProvider的东西.我已经在使用事件而不是异常来记录错误.知道如何从代码隐藏文件中标记相应的失败字段吗?

I am actually stuck on how to map the error message to the field. That's why i was looking for something like ErrorProvider. I am already using events instead of exceptions to record errors. Any idea how to mark the corresponding failed field from the codebehind file?

任何帮助表示赞赏.

推荐答案

我们过去所做的只是在DataContext上有一个错误集合,将其扩展只是添加了List<ValidationError>之类的内容.然后,您需要做的就是覆盖SubmitChanges()并检查是否有任何验证错误,然后决定中止,抛出它们,然后处理您真正希望的那件事...全部在调用base.SubmitChanges()

What we have done in the past is simply have an error collection on the DataContext, extend it just adding something like a List<ValidationError>. Then all you need to do is override SubmitChanges() and check if you have any validation errors, and decide to abort, throw them, handle however you wish really at that point...all before calling base.SubmitChanges()

我们处于ASP.Net每个请求的生命周期中,但是如果您的Context较长,请确保清除错误列表.

We're in a ASP.Net per-request life-cycle, but if your Context is around longer make sure to clear the error list.

ValidationError类/对象包含对所有类都实现的公共基址或接口的引用很方便,因此以后可以根据需要从错误中指向该对象. (例如,获取用于将错误标签或其他信息丢到正确位置的ID).

It's handy for your ValidationError class/objects to contain a reference to a common base or interface that all your classes implement so you can point to the object later from the error if needed. (e.g. get the ID for throwing the error labels or other info in the right place).

示例类:

public class ValidationError {
  public string Message { get; set; }
  public IBase { get; set; }
}

public interface IBase {
  public long ID { get; set; }
  public DateTime DateModified { get; set; }
}

这篇关于用于ASP.net&amp;的ErrorProvider(来自Windows窗体) linq到sql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 05:05