1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Validate Data Using Validation Controls docx

6 370 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Validate Data Using Validation Controls
Định dạng
Số trang 6
Dung lượng 35,27 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

5.2 Validate Data Using Validation Controls I want to be able to validate various types of data entry without having to wait for an error to come back from the server.. These validation

Trang 1

5.2 Validate Data Using Validation Controls

I want to be able to validate various types of data entry without having to wait for an error to come back from the server Can I validate my data on my Web Form?

Note

A note of caution: Through the writing and tech editing of this chapter, there have been some inconsistencies noted in the reaction of some of the validator controls The tech editor and I both contend that this area might have some NET bugs This is also the reason for the lack of coverage in the CustomValidator

Technique

One new feature found in ASP.NET is the inclusion of Validation Web server controls These validation controls allow you to specify other controls of which you want to

validate based on data entered into the controls You can then do the following:

• Have the Validation control display an error message

• Test at a page level to see if all controls that are being validated are valid

• Display a list of error messages for all the controls that are being validated

Available Validation Web Server Controls

You will find the controls for validation in the toolbox, and can see them listed in Table 5.2, with a description of what they validate

Table 5.2 Validation Web Server Controls Control Description

RequiredFieldValidator Validates whether the specified control has been left blank CompareValidator Compares the values in two controls and validates whether

they are equal

RangeValidator Checks to see if the value entered into a control falls

within a range that is specified

RegularExpressionValidator Compares a value entered into a control to see if it matches

an entered mask, such as 999-99-9999, which is the U.S Social Security Number

Trang 2

CustomValidator Allows you to create custom functions on both the client

and server side for validation

ValidationSummary Used to consolidate the messages that all validation

controls return on a page into a list-like format

The main properties you will set on a validation control are FieldToValidate and

ErrorMessage

Note

The CustomValidator control is different in that you will create functions

to perform the validation Other than these, the validation controls require no coding unless you want to validate the whole page

Testing Page Validation in Code

Depending on what is required, you can test for validation at the page level using the Page.Validate method When you call this method, all the Validation controls are

retested, and the Page.IsValid property is set You can use the IsValid property in a condition statement to perform tasks based on the value of the IsValid property, as shown

in the last step of this How-To

Steps

Open and run the Visual Basic NET-Chapter 5 solution From the main page, click on the hyperlink with the caption How-To 5.2: Validate Data Using Validation Controls When the Web Form loads, you will see a number of text boxes with instructions on how

to see the various validation checks Here is the Web Form in Design view (see Figure 5.2)

1 Create a Web Form Then place the controls listed in Table 5.3 and shown in Figure 5.2 with the following properties set

Table 5.3 Control Property Settings for Validation Controls Web Form

Trang 3

Example (Leave Blank to Test)

TextBox ID txtRequiredExample

Example (Enter Two Different Values in These Text Boxes)

TextBox ID txtCompareFirst

TextBox ID txtCompareSecond

(Enter a Number Outside the Range 1 and 10) TextBox ID txtRangeExample

Validator Example (Enter a SSN not matching 999-99-9999)

TextBox ID txtRegularExprExample

(Enter a State Other Than

WA or CA) TextBox ID txtCustomExample

RequiredFieldValidator

Control

ToValidate txtRequiredExample

ErrorMessage Here is the message for the

Required Field Validator CompareValidator ControlToCompare txtCompareFirst

Trang 4

ControlToValidate txtCompareSecond

ErrorMessage Here is the message for the

Compare Validator RangeValidator ControlToValidate txtRangeExample

ErrorMessage Here is the message for the

Range Validator RegularExpressionValidator ControlToValidate txtRegularExprExample ValidationExpression \d{3}-\d{2}-\d{4}

ErrorMessage Here is the message for the

Regular Expression Validator

ValidationSummary HeaderText Error Summary List

2 Note

For the ValidationExpression property of the RegularExpressionValidator, you will want to click the builder button beside the property and choose the SSN from the list of expressions

3 Add the code in Listing 5.4 to the Click event of btnTestValidator You can see an example here of how you might use the Validate method for the page, and act based on when the IsValid property is set

Listing 5.4 wfrmHowTo5_2.aspx.vbs Testing to See If All Controls That the Validation Controls Handle Are Valid

Private Sub btnTestValidators_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles btnTestValidators.Click

Trang 5

' Forces the Validation controls to validate

Page.Validate()

If Page.IsValid Then

' Code if all controls are valid

Else

' Code if even one of the controls isn't valid

End If

End Sub

Figure 5.2 These controls aren't seen on your Web forms unless a validation error

occurs

Comments

Trang 6

You normally wouldn't have the ValidationSummary control on the same page if you were listing the individual error messages using the Validation controls It is nice to be able to have all the errors show up in a list sometimes

Tip

You can affect the way the list looks in the ValidationSummary control

by setting the DisplayMode property You have the choice of using a list,

a bulleted list, or a paragraph

You can also display a message box instead of a list by setting the ShowMessageBox property to True and the ShowSummary property to False

Note

You can display one message in the individual validation controls and another in the ValidationSummary control The ErrorMessage property is reflected in the ValidationSummary list, whereas the Text property of the individual validation controls displays something different if it's set

Ngày đăng: 24/12/2013, 06:17

TỪ KHÓA LIÊN QUAN

w