控件为了比较两个控件的值,此时我们需要使用CompareValidator 控件。
在下面的这个例子中,我们将讲解CompareValidator 控件的用法。
先看文件validata4.aspx:
<!--源文件:form\web页面简介\validate4.aspx-->
<% Page clienttarget=downlevel %>
<html>
<title>CompareValidator控件示例</title>
<head>
<script language="VB" runat="server">
Sub Button1_OnSubmit(sender As Object, e As EventArgs)
If Page.IsValid Then
lblOutput.Text = "比较正确!"
Else
lblOutput.Text = "比较不正确!"
End If
End Sub
Sub lstOperator_SelectedIndexChanged(sender As Object, e As EventArgs)
comp1.Operator = lstOperator.SelectedIndex
comp1.Validate
End Sub
</script>
</head>
<body>
<center>
<h3><font face="Verdana">CompareValidator控件示例</font></h3>
<form runat=server>
<table bgcolor="#eeeeee" cellpadding=10>
<tr valign="top">
<td>
<h5><font face="Verdana">字符串 1:</font></h5>
<asp:TextBox Selected id="txtComp" runat="server"></asp:TextBox>
</td>
<td>
<h5><font face="Verdana">比较运算符:</font></h5>
<asp:ListBox id="lstOperator" OnSelectedIndexChanged="lstOperator_SelectedIndexChanged" runat="server">
<asp:ListItem Selected Value="Equal" >=</asp:ListItem>
<asp:ListItem Value="NotEqual" ><></asp:ListItem>
<asp:ListItem Value="GreaterThan" >></asp:ListItem>
<asp:ListItem Value="GreaterThanEqual" >>=</asp:ListItem>
<asp:ListItem Value="LessThan" ><</asp:ListItem>
<asp:ListItem Value="LessThanEqual" >=<</asp:ListItem>
</asp:ListBox>
</td>
<td>
<h5><font face="Verdana">字符串 2:</font></h5>
<asp:TextBox id="txtCompTo" runat="server"></asp:TextBox><p>
<asp:Button runat=server Text="验证" ID="Button1" onclick="Button1_OnSubmit" />
</td>
</tr>
</table>
<asp:CompareValidator id="comp1" ControlToValidate="txtComp" ControlToCompare = "txtCompTo" Type="String" runat="server"/>
<asp:Label ID="lblOutput" Font-Name="verdana" Font-Size="10pt" runat="server"/>
</form>
</center>
</body>
</html>
在上面的代码中,我们实现了对两个控件的值进行比较。
验证控件介绍--CompareValidator
80酷酷网 80kuku.com