2014-01-31 31 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鏈接太...