CustomValidator控件用法

虽然大部分时间一直从事asp.net的开发,对于一些常用的asp.net服务器端验证控件及它们的组合使用比较熟悉,如:
CompareValidator ——比较验证控件
RangeValidator ——范围验证控件
RegularExpressionValidator ——正则验证控件
RequiredFieldValidator ——必填验证控件
但是一直没去研究CustomValidator控件的用法,心中自然也有一种想法:好不容易从写js验证的痛苦中摆脱出来,如今是能不回去就不要回去了,但是有时候又会遇到一些用上面提到的验证控件没有办法做到的情况(当然是指在客户端没办法做到,我要是说在服务器端没办法做到就让大家笑掉大牙了),用自然想它在客户端完成验证了,至少也要先在客户端验证一下,通过客户端验证再到服务器端验证,这样减少数据的往返时间,降低网络流量和保证反应及时性。

CustomValidator 是一个提供灵活验证方式的控件,它也能在客户端和服务器端验证,分别提供了两种验证的方法原型:
服务器端验证:

void ServerValidation (object source, ServerValidateEventArgs args)

客户端验证(js):

function ValidationFunctionName(source, arguments)

无论对于客户端验证还是服务器端验证,设置通过验证的办法就是将第二个参数的IsValid属性设置为true即可,反之设置为false。

下面我以两个例子来讲述如何使用CustomValidator 来进行验证,第一个用法是用来验证TextBox,第二个用法是验证CheckBoxList,确保其中只能选中一项,而且必须选中一项,对于TextBox这类的控件,我这里设置了对应的CustomValidator 的ValidateEmptyText="True"属性,这样即使控件值为空也能得到验证。

全部代码如下(这里是cs代码和html代码混合的模式):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <meta name="keywords" content="CustomValidator控件用法" />
    <meta name="description" content="周公讲述CustomValidator控件用法" />
    <title>CustomValidator控件用法</title>
</head>
<script language="javascript" type="text/javascript">
    function ClientValidateUserName(source, args) {
        //alert(source);source=CustomValidator1,为验证控件
        //alert(args);
        var obj = document.getElementById("txtUserName");
        if ((obj.value == "") || (obj.value.length > 10)) {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }
    }
 
    function CheckCheckBoxList(source, args) {
        var obj = document.getElementById('<%=this.cbAgeRangeList.ID%>'); //返回具有和属性id的值相同或相似的对象集合
        var k, right = false;
        var length = (obj.all.tags('input').length); //返回obj对象里具有“input”标签对象的集合
        for (k = 0; k < length; k++) {
            //alert(" name:"+obj.all.tags('input')[k].name+"是否:"+obj.all.tags('input')[k].checked);
            if (obj.all.tags('input')[k].checked) // obj对象里具有input标签对象键值为k的属性为checked的值
            {
                right = !right;
            }
 
        }
        if (right) {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }
</script>
<script runat="server">
    protected void Page_Load(Object Src, EventArgs E)
    {
        if (!IsPostBack) DataBind();
    }
    public void ServerValidateUserName(object source, ServerValidateEventArgs args)
    {
        if ((string.IsNullOrEmpty(txtUserName.Text)) || (txtUserName.Text.Length > 10))
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
</script>
<body>
    <form id="Form1" runat="server">
    UserName:<asp:TextBox ID="txtUserName" runat="server" />
    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="错误信息" ControlToValidate="txtUserName"
        ClientValidationFunction="ClientValidateUserName" ValidateEmptyText="True"></asp:CustomValidator>
    <asp:Button ID="btnValid1" runat="server" Text="验证用户名" />
    <asp:CheckBoxList ID="cbAgeRangeList" runat="server" Height="36px" Width="400px"
        RepeatDirection="Vertical">
        <asp:ListItem Value="1"><16</asp:ListItem>
        <asp:ListItem Value="2">16-22</asp:ListItem>
        <asp:ListItem Value="3">22-30</asp:ListItem>
        <asp:ListItem Value="4">30-40</asp:ListItem>
        <asp:ListItem Value="5">40-50</asp:ListItem>
        <asp:ListItem Value="6">50-60</asp:ListItem>
        <asp:ListItem Value="6">60-80</asp:ListItem>
        <asp:ListItem Value="6">>80</asp:ListItem>
    </asp:CheckBoxList>
    <asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="请正确选择年龄段"
        ClientValidationFunction="CheckCheckBoxList"></asp:CustomValidator>
    </form>
</body>
</html>

  

posted @   每天进步多一点  阅读(5001)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
点击右上角即可分享
微信分享提示