2014-01-31 37 views
1
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,多个文本框一个验证属性由VAB ENTLIB 5.0

我我在验证三个文本通过验证应用程序块(ENTLIB 5.0)创建三个字符串验证房产拥有代码并保存在web.cofig文件

帮助我从这个问题

临屋nks提前

回答

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

或者你可以reffer this链接太...