频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

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

来源: 作者:佚名 出处:巧巧读书 2008-02-11 进入讨论组
下一页 1 2 

    摘要:也许大家都已经习惯了使用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页面,如下图所示:


 

验证程序块与ASP.NET的集成(图一)

点击查看大图


 

代码如下:

<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>&nbsp; <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" />&nbsp; <asp:Label ID="validationResultsLabel" runat="server"></asp:Label></td> </tr> </table> </div>
保留地址 http://www.qqread.com/aspdotnet/b396745.html 更多文章 更多内容请看.NET移动与嵌入式技术.NET开发手册ASP.NET教程专题,或进入讨论组讨论。
下一页 1 2 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章