Enterprise Library 3.0体验(4):Validation Application Block与ASP.NET的集成

摘要:也许大家都已经习惯了使用ASP.NET中的验证控件进行数据有效性的验证,但是验证控件的验证无法与我们的自定义的实体类结合起来,两者属于不同层面的验证。在Enterprise Library 3.0中有了Validation Application Block,可以轻松的实现页面验证与类验证的结合。

 

1.编写实体类,本文我使用Enterprise Library 3.0 QuickStarts中的例子,采用Atteribute验证的方式(采用配置文件来实现也是一样的),分别设置验证规则集合、错误信息提示等属性,代码如下:

None.gif public   class  Customer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private string firstName;
InBlock.gif    
private string lastName;
InBlock.gif    
private DateTime dateOfBirth;
InBlock.gif    
private string email;
InBlock.gif    
private int rewardPoints;
InBlock.gif
InBlock.gif    [StringLengthValidator(
150, Ruleset = "RuleSetA", MessageTemplate="First Name must be between 1 and 50 characters long")]
InBlock.gif    
public string FirstName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn firstName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ firstName = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [StringLengthValidator(
150, Ruleset = "RuleSetA", MessageTemplate = "Last Name must be between 1 and 50 characters long")]
InBlock.gif    
public string LastName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn lastName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ lastName = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [RelativeDateTimeValidator(
-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, Ruleset = "RuleSetA", MessageTemplate="Must be 18 years old")]
InBlock.gif    
public DateTime DateOfBirth
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn dateOfBirth; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ dateOfBirth = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [RegexValidator(
@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", Ruleset = "RuleSetA")]
InBlock.gif    
public string Email
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn email; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ email = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [Int32RangeValidator(
01000000, Ruleset = "RuleSetA", MessageTemplate = "Rewards points cannot exceed 1,000,000")]
InBlock.gif    
public int RewardPoints
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn rewardPoints; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ rewardPoints = value; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

2.添加PropertyProxyValidator控件。在工具箱中添加新项,选择Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet,默认的安装路径为C:\Program Files\Microsoft Enterprise Library 3.0 - January 2007 CTP\Bin,添加完成后可以在工具箱中看到PropertyProxyValidator控件。

3.编写ASPX页面,如下图所示:

代码如下:

None.gif < div >
None.gif    
< h3 >
None.gif        Validation Application Block: ASP.NET Integration QuickStart
</ h3 >
None.gif    
None.gif        
< table >
None.gif            
< tr >
None.gif                
< td  style ="width: 100px" >
None.gif                    First Name:
</ td >
None.gif                
< td  style ="width: 508px" >
None.gif                    
< asp:TextBox  ID ="firstNameTextBox"  runat ="server"  Width ="235px" ></ asp:TextBox > &nbsp;
None.gif                    
< br  />
None.gif                
</ td >
None.gif            
</ tr >
None.gif            
< tr >
None.gif                
< td  style ="width: 100px; height: 21px" >
None.gif                    Last Name:
</ td >
None.gif                
< td  style ="width: 508px; height: 21px" >
None.gif                    
< asp:TextBox  ID ="lastNameTextBox"  runat ="server"  Width ="235px" ></ asp:TextBox >< br  />
None.gif                    
</ td >
None.gif            
</ tr >
None.gif            
< tr >
None.gif                
< td  style ="width: 100px" >
None.gif                    Date Of Birth:
</ td >
None.gif                
< td  style ="width: 508px" >
None.gif                    
< asp:TextBox  ID ="dateOfBirthTextBox"  runat ="server" ></ asp:TextBox >< br  />
None.gif                    
</ td >
None.gif            
</ tr >
None.gif            
< tr >
None.gif                
< td  style ="width: 100px" >
None.gif                    E-mail:
</ td >
None.gif                
< td  style ="width: 508px" >
None.gif                    
< asp:TextBox  ID ="emailTextBox"  runat ="server"  Width ="235px" ></ asp:TextBox >< br  />
None.gif                    
</ td >
None.gif            
</ tr >
None.gif            
< tr >
None.gif                
< td  style ="width: 100px; height: 25px;" >
None.gif                    Rewards Points:
</ td >
None.gif                
< td  style ="width: 508px; height: 25px;" >
None.gif                    
< asp:TextBox  ID ="rewardsPointsTextBox"  runat ="server" ></ asp:TextBox >< br  />
None.gif                    
</ td >
None.gif            
</ tr >
None.gif            
< tr >
None.gif                
< td  style ="width: 100px" >
None.gif                
</ td >
None.gif                
< td  style ="width: 508px" >
None.gif                    
< asp:Button  ID ="submitButton"  runat ="server"  Text ="Submit"  OnClick ="submitButton_Click"   /> &nbsp;
None.gif                    
< asp:Label  ID ="validationResultsLabel"  runat ="server" ></ asp:Label ></ td >
None.gif            
</ tr >
None.gif        
</ table >
None.gif
</ div >

4.在页面上需要验证的地方添加PropertyProxyValidator控件,基本的属性设置如下:

None.gif < cc1:propertyproxyvalidator  id ="firstNameValidator"  runat ="server"  
None.gif
None.gif    ControlToValidate
="ContolToValidate"  
None.gif
None.gif    PropertyName
="PropertyName"  
None.gif
None.gif    RulesetName
="RuleSetName"  
None.gif
None.gif    SourceTypeName
="ValidationAspNetQuickStart.Customer" >
None.gif
None.gif
</ cc1:propertyproxyvalidator >

其中ControlToValidate指定对应的需要验证的控件IDPropertyName指定在实体类中的属性名,RulesetName指定验证规则的名称,SourceTypeName指定实体类型名,当然了你也可以像验证控件一样通过Display属性来指定验证信息的显示方式:NoneStaticDynamicPropertyProxyValidator还有一个很重要的事件OnValueConvert,在事件可以通过做类型转换根据是否抛出异常来判断输入是否正确,以及设置验证提示信息等。添加完PropertyProxyValidator后代码如下:

None.gif < div >
None.gif    
< h1 >
None.gif        Validation Application Block: ASP.NET Integration QuickStart
</ h1 >
None.gif    
None.gif        
< table >
None.gif            
< tr >
None.gif                
< td  style ="width: 100px" >
None.gif                    First Name:
</ td >
None.gif                
< td  style ="width: 508px" >
None.gif                    
< asp:TextBox  ID ="firstNameTextBox"  runat ="server"  Width ="235px" ></ asp:TextBox > &nbsp;
None.gif                    
< br  />
None.gif                
< cc1:propertyproxyvalidator  id ="firstNameValidator"  runat ="server"  
None.gif                    ControlToValidate
="firstN
None.gif                    ameTextBox"
 
None.gif                    PropertyName
="FirstName"  
None.gif                    RulesetName
="RuleSetA"  
None.gif                    SourceTypeName
="ValidationAspNetQuickStart.Customer" >
None.gif                
</ cc1:propertyproxyvalidator >
None.gif                
</ td >
None.gif            
</ tr >
None.gif            
< tr >
None.gif                
< td  style ="width: 100px; height: 21px" >
None.gif                    Last Name:
</ td >
None.gif                
< td  style ="width: 508px; height: 21px" >
None.gif                    
< asp:TextBox  ID ="lastNameTextBox"  runat ="server"  Width ="235px" ></ asp:TextBox >< br  />
None.gif                    
< cc1:PropertyProxyValidator  ID ="lastNameValidator"  runat ="server"  ControlToValidate ="lastNameTextBox"
None.gif                        PropertyName
="LastName"  RulesetName ="RuleSetA"  SourceTypeName ="ValidationAspNetQuickStart.Customer" ></ cc1:PropertyProxyValidator ></ td >
None.gif            
</ tr >
None.gif            
< tr >
None.gif                
< td  style ="width: 100px" >
None.gif                    Date Of Birth:
</ td >
None.gif                
< td  style ="width: 508px" >
None.gif                    
< asp:TextBox  ID ="dateOfBirthTextBox"  runat ="server" ></ asp:TextBox >< br  />
None.gif                    
< cc1:PropertyProxyValidator  ID ="dateOfBirthValidator"  runat ="server"  ControlToValidate ="dateOfBirthTextBox"
None.gif                        OnValueConvert
="dateOfBirthValidator_ValueConvert"  PropertyName ="DateOfBirth"
None.gif                        RulesetName
="RuleSetA"  SourceTypeName ="ValidationAspNetQuickStart.Customer" ></ cc1:PropertyProxyValidator ></ td >
None.gif            
</ tr >
None.gif            
< tr >
None.gif                
< td  style ="width: 100px" >
None.gif                    E-mail:
</ td >
None.gif                
< td  style ="width: 508px" >
None.gif                    
< asp:TextBox  ID ="emailTextBox"  runat ="server"  Width ="235px" ></ asp:TextBox >< br  />
None.gif                    
< cc1:PropertyProxyValidator  ID ="emailValidator"  runat ="server"  ControlToValidate ="emailTextBox"
None.gif                        PropertyName
="Email"  RulesetName ="RuleSetA"  SourceTypeName ="ValidationAspNetQuickStart.Customer" ></ cc1:PropertyProxyValidator ></ td >
None.gif            
</ tr >
None.gif            
< tr >
None.gif                
< td  style ="width: 100px; height: 25px;" >
None.gif                    Rewards Points:
</ td >
None.gif                
< td  style ="width: 508px; height: 25px;" >
None.gif                    
< asp:TextBox  ID ="rewardsPointsTextBox"  runat ="server" ></ asp:TextBox >< br  />
None.gif                    
< cc1:PropertyProxyValidator  ID ="rewardPointsValidator"  runat ="server"  ControlToValidate ="rewardsPointsTextBox"
None.gif                         PropertyName
="RewardPoints"
None.gif                        RulesetName
="RuleSetA"  SourceTypeName ="ValidationAspNetQuickStart.Customer"  OnValueConvert ="rewardsPointsValidator_ValueConvert" ></ cc1:PropertyProxyValidator ></ td >
None.gif            
</ tr >
None.gif            
< tr >
None.gif                
< td  style ="width: 100px" >
None.gif                
</ td >
None.gif                
< td  style ="width: 508px" >
None.gif                    
< asp:Button  ID ="submitButton"  runat ="server"  Text ="Submit"  OnClick ="submitButton_Click"   /> &nbsp;
None.gif                    
< asp:Label  ID ="validationResultsLabel"  runat ="server" ></ asp:Label ></ td >
None.gif            
</ tr >
None.gif        
</ table >
None.gif
</ div >

5.在这里有两个验证器用到了OnValueConvert事件,对应的CS代码如下:

None.gif protected   void  rewardsPointsValidator_ValueConvert( object  sender, Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet.ValueConvertEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
string value = e.ValueToConvert as string;
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        e.ConvertedValue 
= Int32.Parse(value);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        e.ConversionErrorMessage 
= "Rewards points is not a valid integer";
InBlock.gif        e.ConvertedValue 
= null;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
protected   void  dateOfBirthValidator_ValueConvert( object  sender, Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet.ValueConvertEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
string value = e.ValueToConvert as string;
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        e.ConvertedValue 
= DateTime.Parse(value, System.Globalization.CultureInfo.CurrentCulture);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        e.ConversionErrorMessage 
= "Date Of Birth is not in the correct format.";
InBlock.gif        e.ConvertedValue 
= null;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

6.运行后,输入错误的数据如下图所示:

关于Validation Application BlockASP.NET的集成就简单得介绍到这儿。

注意本文使用的版本是Enterprise Library 3.0 January 2007 CTP版本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值