First Name
<asp:TextBox ID="TextBox1"
runat="server"
Width="128px">
</asp:TextBox> <br />
Last Name
<asp:TextBox ID="TextBox2"
runat="server"
Width="128px">
</asp:TextBox> <br />
Location
<asp:TextBox ID="TextBox3"
runat="server"
Width="128px">
</asp:TextBox> <br />
<asp:Button ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Validate" />
单击Button Validate
它应该使用单个字符串验证器验证以上三个文本框来自验证应用程序块使用代码基础中的实体库5.0,
我在验证三个文本时有代码通过在验证应用程序块(ENTLIB 5.0)中创建三个字符串验证器属性并将其保存在web.cofig文件中
帮我解决这个问题
提前致谢
答案 0 :(得分:1)
以下是将验证程序与对象关联,然后验证该对象的简单示例。它假定您将通过Unity容器解析MyExample类以注入Validation Application Block ValidatorFactory类的实例。 该代码创建一个新的Customer对象,并使用Validation Application Block Facade对其进行验证。由于应用了字符串长度验证器属性,因此块会检查客户名称的长度是否在0到20个字符之间。在这种情况下,客户名称是非法的,因为它太长。应用程序抛出异常,通知您错误。
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
public class Customer
{
[StringLengthValidator(0, 20)]
public string CustomerName;
public Customer(string customerName)
{
this.CustomerName = customerName;
}
}
public class MyExample
{
private ValidatorFactory factory;
public MyExample(ValidatorFactory valFactory)
{
factory = valFactory;
}
public void MyMethod()
{
Customer myCustomer = new Customer("A name that is too long");
Validator<Customer> customerValidator
= factory.CreateValidator<Customer>();
// Validate the instance to obtain a collection of validation errors.
ValidationResults r = customerValidator.Validate(myCustomer);
if (!r.IsValid)
{
throw new InvalidOperationException("Validation error found.");
}
}
}
来源为MSDN
或者您也可以提供this链接...