驗證控件介紹--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>
在上面的代碼中,我們實現了對兩個控件的值進行比較。