3.6使用CompareValidator控件

使用CompareValidator控件

三种不同类型的验证任务
1、执行数据类型检测  验证输入是否类型正确的值
2、输入值和一固定值进行比较
3、一表单值与另一表单值进行比较

六属性:
ControlToValidate
Text
Type     String  Integer  Double  Date Currency
Operator  所执行的比较类型 可能的值:DataTypeCheck  Equal GreaterThan GreaterThanEqual LessThan  LessThanEqual和NotEqual
ValueToCompare  所比较的固定值
ControlToCompare 所比较的控件的ID

code

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    protected void btnSubmit_Click(object sender, EventArgs e)
    {

    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Show Data Type Check</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblBirthDate" Text="Birth Date:" AssociatedControlID="txtBirthDate"
            runat="server" />
        <asp:TextBox ID="txtBirthDate" runat="server" />
        <asp:CompareValidator ID="cmpBirthDate" Text="(Invalid Date)" ControlToValidate="txtBirthDate"
            Type="Date" Operator="DataTypeCheck" runat="server" />
        <br />
        <br />
        <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" />
    </div>
    </form>
</body>
</html>

----------------------------------------
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        cmpDate.ValueToCompare = DateTime.Now.ToString("d");
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Show Fixed Value</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblDate" AssociatedControlID="txtDate" runat="server" />
        <asp:TextBox ID="txtDate" runat="server" />
        <asp:CompareValidator ID="cmpDate" Text="(Date must be greater thann now)" ControlToValidate="txtDate"
            Type="Date" Operator="GreaterThan" runat="server" />
        <br />
        <br />
        <asp:Button ID="btnSubmit" Text="Submit" runat="server" />
    </div>
    </form>
</body>
</html>

----------------------------------------
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Show Compare Values</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblStartDate" Text="Start Date:" runat="server" />
        <asp:TextBox ID="txtStartDate" runat="server" />
        <br />
        <br />
        <asp:Label ID="lblEndDate" Text="End Date:" runat="server" />
        <asp:TextBox ID="txtEndDate" runat="server" />
        <asp:CompareValidator ID="cmpDate" Text="(End date must be greater than start date)"
            ControlToValidate="txtEndDate" ControlToCompare="txtStartDate" Type="Date" Operator="GreaterThan"
            runat="server" />
        <br />
        <br />
        <asp:Button ID="btnSubmit" Text="Submit" runat="server" />
    </div>
    </form>
</body>
</html>

2011-4-26 15:08 danny

你可能感兴趣的:(html,C++,c,XHTML,C#)