摘要:也许大家都已经习惯了使用ASP.NET中的验证控件进行数据有效性的验证,但是验证控件的验证无法与我们的自定义的实体类结合起来,两者属于不同层面的验证。在Enterprise Library 3.0中有了Validation Application Block,可以轻松的实现页面验证与类验证的结合。
1.编写实体类,本文我使用Enterprise Library 3.0 QuickStarts中的例子,采用Atteribute验证的方式(采用配置文件来实现也是一样的),分别设置验证规则集合、错误信息提示等属性,代码如下:
public class Customer

{
private string firstName;
private string lastName;
private DateTime dateOfBirth;
private string email;
private int rewardPoints;

[StringLengthValidator(1, 50, Ruleset = "RuleSetA", MessageTemplate="First Name must be between 1 and 50 characters long")]
public string FirstName

{

get { return firstName; }

set { firstName = value; }
}

[StringLengthValidator(1, 50, Ruleset = "RuleSetA", MessageTemplate = "Last Name must be between 1 and 50 characters long")]
public string LastName

{

get { return lastName; }

set { lastName = value; }
}

[RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, Ruleset = "RuleSetA", MessageTemplate="Must be 18 years old")]
public DateTime DateOfBirth

{

get { return dateOfBirth; }

set { dateOfBirth = value; }
}

[RegexValidator(@""w+([-+.']"w+)*@"w+([-.]"w+)*"."w+([-.]"w+)*", Ruleset = "RuleSetA")]
public string Email

{

get { return email; }

set { email = value; }
}

[Int32RangeValidator(0, 1000000, Ruleset = "RuleSetA", MessageTemplate = "Rewards points cannot exceed 1,000,000")]
public int RewardPoints

{

get { return rewardPoints; }

set { rewardPoints = value; }
}
} 2.添加PropertyProxyValidator控件。在工具箱中添加新项,选择Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet,默认的安装路径为C:"Program Files"Microsoft Enterprise Library 3.0 - January 2007 CTP"Bin,添加完成后可以在工具箱中看到PropertyProxyValidator控件。
3.编写ASPX页面,如下图所示:

代码如下:
<div>
<h3>
Validation Application Block: ASP.NET Integration QuickStart</h3>
<table>
<tr>
<td style="width: 100px">
First Name:</td>
<td style="width: 508px">
<asp:TextBox ID="firstNameTextBox" runat="server" Width="235px"></asp:TextBox>
<br />
</td>
</tr>
<tr>
<td style="width: 100px; height: 21px">
Last Name:</td>
<td style="width: 508px; height: 21px">
<asp:TextBox ID="lastNameTextBox" runat="server" Width="235px"></asp:TextBox><br />
</td>
</tr>
<tr>
<td style="width: 100px">
Date Of Birth:</td>
<td style="width: 508px">
<asp:TextBox ID="dateOfBirthTextBox" runat="server"></asp:TextBox><br />
</td>
</tr>
<tr>
<td style="width: 100px">
E-mail:</td>
<td style="width: 508px">
<asp:TextBox ID="emailTextBox" runat="server" Width="235px"></asp:TextBox><br />
</td>
</tr>
<tr>
<td style="width: 100px; height: 25px;">
Rewards Points:</td>
<td style="width: 508px; height: 25px;">
<asp:TextBox ID="rewardsPointsTextBox" runat="server"></asp:TextBox><br />
</td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 508px">
<asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="submitButton_Click" />
<asp:Label ID="validationResultsLabel" runat="server"></asp:Label></td>
</tr>
</table>
</div> 4.在页面上需要验证的地方添加PropertyProxyValidator控件,基本的属性设置如下:
<cc1:propertyproxyvalidator id="firstNameValidator" runat="server"

ControlToValidate="ContolToValidate"

PropertyName="PropertyName"

RulesetName="RuleSetName"

SourceTypeName="ValidationAspNetQuickStart.Customer">

</cc1:propertyproxyvalidator> 其中ControlToValidate指定对应的需要验证的控件ID,PropertyName指定在实体类中的属性名,RulesetName指定验证规则的名称,SourceTypeName指定实体类型名,当然了你也可以像验证控件一样通过Display属性来指定验证信息的显示方式:None、Static、Dynamic。PropertyProxyValidator还有一个很重要的事件OnValueConvert,在事件可以通过做类型转换根据是否抛出异常来判断输入是否正确,以及设置验证提示信息等。添加完PropertyProxyValidator后代码如下:
<div>
<h1>
Validation Application Block: ASP.NET Integration QuickStart</h1>
<table>
<tr>
<td style="width: 100px">
First Name:</td>
<td style="width: 508px">
<asp:TextBox ID="firstNameTextBox" runat="server" Width="235px"></asp:TextBox>
<br />
<cc1:propertyproxyvalidator id="firstNameValidator" runat="server"
ControlToValidate="firstN
ameTextBox"
PropertyName="FirstName"
RulesetName="RuleSetA"
SourceTypeName="ValidationAspNetQuickStart.Customer">
</cc1:propertyproxyvalidator>
</td>
</tr>
<tr>
<td style="width: 100px; height: 21px">
Last Name:</td>
<td style="width: 508px; height: 21px">
<asp:TextBox ID="lastNameTextBox" runat="server" Width="235px"></asp:TextBox><br />
<cc1:PropertyProxyValidator ID="lastNameValidator" runat="server" ControlToValidate="lastNameTextBox"
PropertyName="LastName" RulesetName="RuleSetA" SourceTypeName="ValidationAspNetQuickStart.Customer"></cc1:PropertyProxyValidator></td>
</tr>
<tr>
<td style="width: 100px">
Date Of Birth:</td>
<td style="width: 508px">
<asp:TextBox ID="dateOfBirthTextBox" runat="server"></asp:TextBox><br />
<cc1:PropertyProxyValidator ID="dateOfBirthValidator" runat="server" ControlToValidate="dateOfBirthTextBox"
OnValueConvert="dateOfBirthValidator_ValueConvert" PropertyName="DateOfBirth"
RulesetName="RuleSetA" SourceTypeName="ValidationAspNetQuickStart.Customer"></cc1:PropertyProxyValidator></td>
</tr>
<tr>
<td style="width: 100px">
E-mail:</td>
<td style="width: 508px">
<asp:TextBox ID="emailTextBox" runat="server" Width="235px"></asp:TextBox><br />
<cc1:PropertyProxyValidator ID="emailValidator" runat="server" ControlToValidate="emailTextBox"
PropertyName="Email" RulesetName="RuleSetA" SourceTypeName="ValidationAspNetQuickStart.Customer"></cc1:PropertyProxyValidator></td>
</tr>
<tr>
<td style="width: 100px; height: 25px;">
Rewards Points:</td>
<td style="width: 508px; height: 25px;">
<asp:TextBox ID="rewardsPointsTextBox" runat="server"></asp:TextBox><br />
<cc1:PropertyProxyValidator ID="rewardPointsValidator" runat="server" ControlToValidate="rewardsPointsTextBox"
PropertyName="RewardPoints"
RulesetName="RuleSetA" SourceTypeName="ValidationAspNetQuickStart.Customer" OnValueConvert="rewardsPointsValidator_ValueConvert"></cc1:PropertyProxyValidator></td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 508px">
<asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="submitButton_Click" />
<asp:Label ID="validationResultsLabel" runat="server"></asp:Label></td>
</tr>
</table>
</div> 5.在这里有两个验证器用到了OnValueConvert事件,对应的CS代码如下:
protected void rewardsPointsValidator_ValueConvert(object sender, Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet.ValueConvertEventArgs e)

{
string value = e.ValueToConvert as string;
try

{
e.ConvertedValue = Int32.Parse(value);
}
catch

{
e.ConversionErrorMessage = "Rewards points is not a valid integer";
e.ConvertedValue = null;
}
}

protected void dateOfBirthValidator_ValueConvert(object sender, Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet.ValueConvertEventArgs e)

{
string value = e.ValueToConvert as string;
try

{
e.ConvertedValue = DateTime.Parse(value, System.Globalization.CultureInfo.CurrentCulture);
}
catch

{
e.ConversionErrorMessage = "Date Of Birth is not in the correct format.";
e.ConvertedValue = null;
}
} 6.运行后,输入错误的数据如下图所示:

关于Validation Application Block与ASP.NET的集成就简单得介绍到这儿。
注意本文使用的版本是Enterprise Library 3.0 January 2007 CTP版本。
发表评论
#1楼 219.137.137.* 2007-01-23 08:47 |
看起来需要postback才能验证了
@TerryLees 我这边没有办法通过 www.codeplex.com 下载。
如果方便的话 能不能把Enterprise Library 3.0发给我一份
zhaosichao#gmail.com
请教Terry一个问题。你是怎样创作这篇文章的呢?通过在V2.0中已掌握的知识然后探索?或者通过分析V3.0的类库,探索出这些用法?或者阅读现在极为稀少的文档,然后尝试? 麻烦告知我一下可以吗,我想学习一些掌握知识的方法。谢谢:)
#6楼[楼主]2007-01-23 22:45 |
@Minco
@木野狐
我觉得应该是支持的,但我没试出来,也许是没设置对吧,没文档啊:)
#7楼[楼主]2007-01-23 22:45 |
@greatqn
最重要的是两者可以结合起来
#8楼[楼主]2007-01-23 22:46 |
@zhaosichao
太大了,我这边网速慢发不出去……
#9楼[楼主]2007-01-23 22:47 |
@Hunts.C
应该是都有吧,呵呵
现在几乎没什么文档:)
@TerryLee
呵呵,我有体会。先行者总是不容易的,很感谢您能分享这么多新鲜的知识。
学习 IronPython 的时候我就觉得文档,教程什么的真少。只有自己啃源代码来学。
#11楼[楼主]2007-01-23 23:32 |
@木野狐
呵呵,太客气
只好慢慢去摸索了:)
Int32RangeValidator没有这个函数啊
Error 1 The type or namespace name 'Int32RangeValidator' could not be found (are you missing a using directive or an assembly reference?) D:"qing"Enterprise Library 3.0体验"EnterpriseTest"ClassLibrary1"Entity.cs 53 10 ClassLibrary1
在实体类中,最后一个那个Int32RangeValidator
没有这个啊!怎么办啊!!!
Microsoft.Practices.EnterpriseLibrary.Validation.Validators这个命名空间下没有啊!
#15楼 221.246.147.* 2007-06-01 16:30 |
@OctoberOne
Enterprise Library 3.0正式版中Int32RangeValidator已经被取消。
在class里应该用下列代码取而代之。
[RangeValidator(18, RangeBoundaryType.Inclusive, 24, RangeBoundaryType.Inclusive)]
public int Age
{
get { return _age; }
set { _age = value; }
}