您的当前位置:我要购书网>在线阅读>ASP/PHP/.NET/XML

creating your own validation

来源:互联网  作者:  发布:我要购物网收集整理  发布时间:2006-8-31 人气:167

Creating Your Own ValidationThere may be times when you can't find one of the built-in validation controls that handles your specific data. For example, which control could verify that a number the user has entered is an even number (if that was a requirement of your page)? There isn't such a control, but ASP.NET provides the CustomValidator control, which allows you to specify procedures to be run in order to validate the data in the associated input control.For this example, your goal is to ensure that users enter either CA, NV, or AZ into the State text box on the page. You could use a DropDownList control for this, or you could use a RegularExpressionValidator control (setting the expression to be CA|NV|AZ). However, for the sake of this example, suppose you want to take advantage of the CustomValidator control.The CustomValidator control requires you to supply code for the control's ServerValidate event梚t calls this code as it attempts to validate the data on the server. No matter what browser has loaded the page, this code will run before the entire page can be validated. If the browser supports client-side script, it is nice to provide the script to run from within the browser, on the client side, to provide the same sort of experience you get when working with the other validation controls.Therefore, you'll need to write the event procedure, in the page's code-behind file, to provide server-side validation. In addition, in the page itself, you'll need to insert the client-side script that will be sent down to the browser. (In addition, you'll need to set the control's ClientValidationFunction property to indicate the name of the client-side function.) Both procedures receive two parameters: The first parameter contains the object that raised the event (the CustomValidator control itself), and the second parameter is a ServerValidateEventArgs object containing information about the event. You can use the Value property of the second parameter to retrieve the value the user entered, and you'll set the IsValid property of the argument to indicate whether the value is valid.To add validation for the State text box, follow these steps:Add a CustomValidator control adjacent to the State text box.Set the properties for the control as shown in Table 8.6.Table 8.6. Set the CustomValidator Control's Properties to Match These Items Property Value ID cvalState ClientValidationFunction ValidState ControlToValidate txtState Display Dynamic ErrorMessage Enter CA, NV or AZ Double-click the CustomValidator control and modify the ServerValidate procedure so that it looks like this:Private Sub cvalState_ServerValidate( _ ByVal source As System.Object, _ ByVal args As _ System.Web.UI.WebControls.ServerValidateEventArgs) _ Handles cvalState.ServerValidate Select Case args.Value Case "CA", "NV", "AZ" args.IsValid = True Case Else args.IsValid = False End SelectEnd SubTIPThe ServerValidate event handler uses the Value property of its second parameter to determine the value the user has entered and the IsValid property to indicate whether the value is valid.Close the code-behind file and then press Ctrl+PgDn (or use the View, HTML Source menu item) to view the HTML source for the page.Immediately below the element, near the top of the page, add the following script code, which provides the client-side validation:Now that you've added both the server-side and the client-side validation procedures, browse to the page and verify that entering an invalid state does trigger the validation code.
相关文章
· creating user controls
· create为什么可以为虚函数?
· createprocess创建的子进程的命令行参数
· cruisecontrol简介
· cricheditdoc在关闭非cricheditvie..
· createprocess使用心得
· createwindow 函数
· createremotethread简单应用
· create a menu item into the ..
· creating your own validation
热点文章
%>
· creating user controls
· 提高fastreplace速度 (fstrrep.pas)
· asc ii 完整码表及简介
· 自动生成拼音(汉字反查到拼音)
· sql2000无法安装的解决办法
· 浏览器集成教学 自定义浏览器
· vc++技术内幕(第四版)笔记(第7章)
· mysql5.0中文乱码解决方案
· sql server日期计算
· vc下利用ado连接access数据库
 淘宝旺旺:我要购书网上书店『图书目录
本购书中心地址: 杭州市延安路111号清波商厦南楼D座(总部) 上海市闸北区老沪太路网上购书中心(沪部),  邮编:310002
电子邮件:books@51goushu.com  经营许可证编号:沪ICP备06038574号
版权所有 2003-2008 © All Rights Reserved .购书网