技术开发 频道

验证程序块与ASP.NET的集成



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

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

    其中ControlToValidate指定对应的需要验证的控件IDPropertyName指定在实体类中的属性名,RulesetName指定验证规则的名称,SourceTypeName指定实体类型名,当然了你也可以像验证控件一样通过Display属性来指定验证信息的显示方式:NoneStaticDynamicPropertyProxyValidator还有一个很重要的事件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>&nbsp; <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" />&nbsp; <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 BlockASP.NET的集成就简单得介绍到这儿。

0
相关文章