分享
 
 
 

Checking a Range

王朝asp·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

Checking a Range

The RangeValidator control allows you to verify that the value of a control

falls within a specified range. For example, you could make sure that a

text box's value is between 5 and 10 or between a month ago and the current

date.

Normally, you'll need to set the properties shown in Table 8.3, in addition

to those listed in Table 8.1.

Table 8.3. Set These Properties for RangeValidator Controls Property

Description

MaximumValue The maximum allowable value.

MinimumValue The minimum allowable value.

Type The type of data to compare, selected from String, Integer, Date,

Double, or Currency. This property controls the type of comparison to

perform梚t wouldn't make sense to perform a text comparison when you're

entering numeric values, for example. If you did, "2" would be greater than

"11." Select the correct data type to perform the correct type of

comparison.

In order to try out this control, you will modify the sample page,

EmpMaint.aspx, adding a RangeValidator control to ensure that the employee

birth date falls between 1/1/1900 and 12/31/1984. Here are the steps to

follow:

Click to the right of the RequiredFieldValidator control that's associated

with the BirthDate field, moving the selection point to that location.

In the Toolbox, double-click the RangeValidator control to add it adjacent

to the existing RequiredFieldValidator control.

Set the properties for the control as shown in Table 8.4.

Table 8.4. Set These Properties for the RangeValidation Control Property

Value

ID rvalBirthdate

ControlToValidate txtBirthDate

ErrorMessage Birth Date must be between 1/1/1900 and 12/31/1984

MaximumValue 12/31/1984

MinimumValue 1/1/1900

Type Date

Right-click the page and select Build and Browse from the context menu.

Enter an invalid date for the Birth Date field (perhaps a date after

12/31/1984) and then press Tab to leave the control. You should see an

error appear immediately.

Close the browser window.

TIP

Although we've used dates formatted correctly for the United States here,

you'll need to take into account the current locale when testing your

pages. If your date format is different from ours, make sure you attempt to

validate dates that are correct for the locale of the browser.

Although you did see the correct error message, you may have noticed that

it didn't appear immediately to the right of the associated text box.

Because you had already placed another validation control adjacent to the

Birth Date text box, that control "consumes" the space where you'd expect

to see the out-of-range error message.

To solve this problem, you can use the Display property of the validation

controls. This property has three values: None, Dynamic, and Static. The

default value is Static, indicating that the control always takes up the

same amount of space on the rendered page. Selecting None causes the

control to take up no space and never display any text (unless you add a

ValidationSummary control to the page). Selecting Dynamic allows the

control to take up no space if it isn't displaying any text. This choice,

then, will fix the problem. Simply follow these steps:

Select each of the validation controls on the page and set the Display

property for each to Dynamic.

Repeat the previous experiment and prove to yourself that the display works

better now.

TIP

If you only want to validate that the user has entered a particular data

type (a date, for example) but don't care about the specific value, you can

use the CompareValidator control, discussed later in the chapter.

If you want to provide a dynamic range for the RangeValidator control (for

example, allowing dates to be entered for the Birth Date field between 70

years ago and 17 years ago, figuring that you can't hire anyone older than

70 or younger than 17), you'll need to write a little code. You might want

to modify the EmpMaint.aspx page so that it adds these restrictions as it

loads.

Modify the Page_Load procedure of EmpMaint.aspx so that it looks like this:

Private Sub Page_Load( _

ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then

With rvalBirthDate

.MinimumValue = CStr(Today.AddYears(-70))

.MaximumValue = CStr(Today.AddYears(-17))

.ErrorMessage = String.Format( _

"Enter a date between {0} and {1}", _

.MinimumValue, .MaximumValue)

End With

End If

End Sub

Browse the page again and try entering a birth date that's more than 70

years ago or less than 17 years ago. You'll see the error message,

indicating the valid range of dates.

The previous example brings up some interesting ideas:

You can use the AddYears method of a date to add or subtract years.

The MinimumValue and MaximumValue properties of the RangeValidator control

both accept String values梱ou'll need to convert anything you place in

these properties into strings.

You can dynamically modify the ErrorMessage text of a control.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有