rangeValidator控件用于检测表单字段的值是否在指定的最小值和最大值之间。使用这个控件时,必须设置5个属性:
q ControlToValidate——被验证的表单字段的ID。
q Text——验证失败时显示的错误信息。
q MinimumValue——验证范围的最小值。
q MaximumValue——验证范围的最大值。
q Type——所执行的比较类型。可能的值有String、Integer、Double、Date和Currency
<%@ Page Language="C#" %>
2
3 <script runat="server">
4 protected void Page_Load(object sender, EventArgs e)
5 {
6 RangeValidator1.MinimumValue = DateTime.Now.ToShortDateString();
7 RangeValidator1.MaximumValue = DateTime.Now.AddDays(14).ToShortDateString();
8 }
9
10 protected void Calendar1_SelectionChanged(object sender, EventArgs e)
11 {
12 TextBox1.Text = Calendar1.SelectedDate.ToShortDateString();
13 }
14
15 protected void Button1_Click(object sender, EventArgs e)
16 {
17 if (Page.IsValid)
18 {
19 Label1.Text = "You are set to arrive on: " + TextBox1.Text.ToString();
20 }
21 }
22 </script>
23
24 <html xmlns="http://www.111cn.net" >
25 <head id="Head1" runat="server">
26 <title>Date Validation Check</title>
27 </head>
28 <body>
29 <form id="form1" runat="server">
30 Arrival Date:
31 <asp教程:TextBox ID="TextBox1" runat="server"></asp:TextBox>
32 <asp:RangeValidator ID="RangeValidator1" runat="server" Text="You must only select a date within the next two weeks."
33 ControlToValidate="TextBox1" Type="Date"></asp:RangeValidator><br />
34 <br />
35 Select your arrival date:<br />
36 <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
37
38 <br />
39 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
40 <br />
41 <br />
42 <asp:Label ID="Label1" runat="server"></asp:Label>
43 </form>
44 </body>
45 </html>
RangeValidator控件主要界定输入的值的范围。因为有时我们要求输入的值是要有一定范围的,所以我们要使用RangeValidator来判断。
在下面的这个例子中,我们将来介绍RangeValidator控件。
请看validata5.aspx的程序内容:
<!--源文件:formweb页面简介validate5.aspx-->
<%@ Page clienttarget=downlevel %>
<html>
<center>
<title>RangeValidator控件演示</title>
<head>
<script language="VB" runat="server">
Sub Button1Click(sender As Object, e As EventArgs)
If (Page.IsValid) Then
lblOutput.Text = "结果正确!"
Else
lblOutput.Text = "结果不正确!"
End If
End Sub
Sub lstOperatorSelectedIndexChanged(sender As Object, e As EventArgs)
rangeVal.Type = lstType.SelectedIndex
rangeVal.Validate
End Sub
</script>
</head>
<body><h3><font face="Verdana">RangeValidator控件演示</font></h3>
<p>
<form runat="server">
<table bgcolor="#eeeeee" cellpadding=10>
<tr valign="top">
<td>
<h5><font face="Verdana">输入要验证的值:</font></h5>
<asp:TextBox Selected id="txtComp" runat="server"/>
</td>
<td>
<h5><font face="Verdana">数据类型:</font></h5>
<asp:DropDownList id="lstType" OnSelectedIndexChanged="lstOperatorSelectedIndexChanged" runat=server>
<asp:ListItem Selected Value="String" >String</asp:ListItem>
<asp:ListItem Value="Integer" >Integer</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<h5><font face="Verdana">最小值:</font></h5>
<asp:TextBox id="txtMin" runat="server" />
</td>
<td>
<h5><font face="Verdana">最大值:</font></h5>
<asp:TextBox id="txtMax" runat="server" /><p>
<asp:Button Text="验证" ID="Button1" onclick="Button1Click" runat="server" />
</td>
</tr>
</table>
<asp:RangeValidator id="rangeVal" Type="String" ControlToValidate="txtComp" MaximumControl="txtMax" MinimumControl="txtMin" runat="server"/>
<br>
<asp:Label id="lblOutput" Font-Name="verdana" Font-Size="10pt" runat="server" />
</form>
</body>
</center>
</html>
当我们在三个文本框中分别输入要验证的值,最大值,和最小值,然后按下验证按钮,页面上将显示判断的结果
使用RangeValidator控件时不要忘记设置Type属性。Type属性的值默认为String,RangeValidator控件执行字符串比较来确定值是否介于最小值和最大值之间。