?? ex16vb.aspx
字號(hào):
<%@ Page clienttarget=downlevel %>
<html>
<title>第十六例 CompareValidator和CustomValidator控件使用</title>
<head>
<script language="VB" runat="server">
Sub Button_Click(sender as Object , E as EventArgs )
Dim rand_number As New Random()
'生成一個(gè)0-10隨機(jī)數(shù)
Compare1.ValueToCompare = rand_number.Next(1, 10).ToString()
'開(kāi)始進(jìn)行比較
Compare1.Validate()
If Page.IsValid Then
lblOutput.Text = "恭喜您,您猜的數(shù)字和隨機(jī)生成的一樣<BR>"
Else
lblOutput.Text = "<BR><BR>對(duì)不起,您猜錯(cuò)了"
End If
'在頁(yè)面上顯示隨機(jī)生成的數(shù)字
lblOutput.Text &= "<br><br>" & "隨機(jī)生成的數(shù)字是: <font color=red size=4>" & Compare1.ValueToCompare & "<BR></font>"
End Sub
'自定義驗(yàn)證函數(shù)
Sub ServerValidation (source As object, args As ServerValidateEventArgs)
try
'取得文本框的數(shù)字,驗(yàn)證是否在1-10之間
Dim num As Integer = Integer.Parse(args.Value)
args.IsValid = (num > 0 And num < 10 )
catch ex as Exception
args.IsValid = false
End try
End Sub
</script>
</head>
<body>
<form runat=server>
<h3><font face="Verdana">第十六例 CompareValidator和CustomValidator控件使用</font></h3>
<h5><font face="Verdana">請(qǐng)您輸入一個(gè)1-10的隨機(jī)數(shù)字:</font></h5>
<asp:TextBox id="TextBox1"
runat="server"/>
<asp:CompareValidator id="Compare1"
ControlToValidate="TextBox1"
ValueToCompare="0"
Type="Integer"
ErrorMessage="猜測(cè)錯(cuò)誤!!"
Text="*"
Display="Dynamic"
runat="server"/>
<br><br>
<asp:Button id="Button1"
Text="驗(yàn)證"
OnClick="Button_Click"
runat="server"/>
<br><br>
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="Textbox1"
ClientValidationFunction="ClientValidate"
OnServerValidate="ServerValidation"
Display="Static"
ErrorMessage="輸入數(shù)據(jù)沒(méi)有在1-10之間!"
ForeColor="green"
BackColor="Red"
Font-Name="verdana"
Font-Size="10pt"
runat="server"/>
<BR>
<asp:Label id="lblOutput"
Font-Name="verdana"
Font-Size="10pt"
runat="server"/>
</form>
</body>
</html>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -