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代码混合的模式):

<%@ 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 @ 2018-06-02 15:12  每天进步多一点  阅读(4949)  评论(0编辑  收藏  举报