Tải bản đầy đủ (.pdf) (6 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (35.27 KB, 6 trang )

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.
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

Object Property Setting
Label Text Required Field Validator
Example (Leave Blank to
Test)
TextBox ID txtRequiredExample
Label Text Compare Validator
Example (Enter Two
Different Values in These
Text Boxes)
TextBox ID txtCompareFirst
TextBox ID txtCompareSecond
Label Text Range Validator Example
(Enter a Number Outside
the Range 1 and 10)
TextBox ID txtRangeExample
Label Text Regular Expression
Validator Example (Enter a
SSN not matching 999-99-
9999)
TextBox ID txtRegularExprExample
Label Text Custom Validator Example
(Enter a State Other Than
WA or CA)
TextBox ID txtCustomExample
Button ID btnTestValidators
Text Test Validators
Label Text Page Validation Errors
RequiredFieldValidator
Control
ToValidate txtRequiredExample

ErrorMessage Here is the message for the
Required Field Validator
CompareValidator ControlToCompare txtCompareFirst
ControlToValidate txtCompareSecond
ErrorMessage Here is the message for the
Compare Validator
RangeValidator ControlToValidate txtRangeExample
MaximumValue 10
MinimumValue 1
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
HyperLink ID hplReturnToMain
NavigateURL wfrmMain.aspx
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

'-- 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

×