验证控件介绍--CompareValidator
2024-07-21 02:22:01
供稿:网友
为了比较两个控件的值,此时我们需要使用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"/>
<br>
<asp:label id="lbloutput" font-name="verdana" font-size="10pt" runat="server"/>
</form>
</center>
</body>
</html>
在上面的代码中,我们实现了对两个控件的值进行比较。