
C# ПІДРУЧНИКИ / c# / Hungry Minds - ASP.NET Bible VB.NET & C#
.pdf</body>
</html>
This style of validation has a few drawbacks. First, if you have several more controls on this page to validate, the validation routine will become quite lengthy and prone to errors. Second, if you delete or rename your controls on the page, you need to update your validation routine. Also, you may want to perform several different types of validation on the same control's value. As you can see, this adds to your validation routine and requires a fair amount of code to implement. Finally, you have your validation code mixed with the presentation code. As you will soon see, .NET's validation controls overcome all of these limitations.
Note You could just as easily have used client-side validation with JScript to save a round trip to the server. However, it is always good practice to validate the values again on the server to prevent any unwanted values from being passed to your server.
Using the RequiredFieldValidator Control
Use the RequiredFieldValidator control when a value is required for an input element on the Web page. This control checks whether the value of the associated input control is different from its initial value. You can easily convert the previous sample ASP code into
.NET with the RequiredFieldValidator control. Take a look at the following code fragment:
<!-- Required Field Validator -->
First<br>
<asp:textbox id="First" maxlength="20" runat="Server"/>
<asp:requiredfieldvalidator
id="rfvFirst"
controltovalidate="First"
display="dynamic"
errormessage="Please enter your first name."
runat="Server">
</asp:requiredfieldvalidator>
<br>
Launch your browser and navigate to Required.aspx code. A screen similar to Figure 6-2 should be displayed.

Figure 6-2: Required field validator control example 1
Before entering a First name, click the Validate button. A screen similar to Figure 6-3 should be displayed.
Figure 6-3: Required field validator control example 2
If all went well, you should see the validation message "Please enter your first name." on the page. Now enter your name and click the Validate button again. This time you should see the message, "Hello, your name" displayed on the page, as shown in Figure 6-4.

Figure 6-4: Required field validator control example 3
Figure 6-4 looks just like the example for "Classic ASP" in Figure 6-1, but underneath the covers, ASP.NET has written the validation code for you.
Now that you have looked at a quick example of using .NET validation, Table 6-1 gives you a more detailed description of the properties for the RequiredFieldValidator control.
Table 6-1: Properties of the RequiredFieldValidator control
Property
ID
Definition
This property gets or sets the identifier for the control. This identifier is used via programm ing to access the control's properties. If an identifier does not exist, then you cannot write handlers for this control.
ControlToValidate |
|
Gets or |
|
|
|
|
|
sets the |
|
|
name of |
|
|
the control |
|
|
to |
|
|
validate. |
|
|
|
Display |
|
Gets or |
|
|

Table 6-1: Properties of the RequiredFieldValidator control
Property
ErrorMessage
RunAt
Definition
sets the display appearanc e of the validator control on the Web page.
Gets or sets the error message displayed for this control.
Specifies that this control runs on the server.
The ControlToValidate and Display properties from Table 6-1 are explained in more detail in the following sections.
ControlToValidate property
Set this property's value to the name of the control to validate. If you refer to the code listing, this property was set to First to indicate that a value is required for this control.
Display property
You can set this property to one of three values to determine how the error message is displayed on the page if validation fails:
§Dynamic: The validator is displayed inline on the Web page if validation fails. The validator only takes up space on the page when the validator is visible. This allows multiple validators to occupy the same physical location on the Web page when those validators become visible. To avoid the Web page layout changing the validator becomes vi sible, the HTML element containing the validator must be large enough to accommodate the size of the validator.
§Static: The validator is displayed inline on the Web page if validation fails. Also, if the validator is hidden and becomes visible, the page layout does not change.
§None: The validation contents are not displayed inline on the Web page, the error message is displayed in the ValidationSummary control.
Using the CompareValidator Control
Use the CompareValidator control to make sure that a value matches a specified value. This control compares the value of an input control to another input control or a constant value using a variety of operators and types. You can also use this control to make sure that your input value is of a specific type: integer, string, and so on. Table 6-2 lists the commonly used properties for the CompareValidator control.
Table 6-2: Properties of the CompareValidator control
Property |
|
Definition |
|
|
|

Table 6-2: Properties of the CompareValidator control
Property
ID
Definition
This property gets or sets the identifier for the control. This identifier is used via programm ing to access the control's properties. If an identifier does not exist, then you cannot write handlers for this control.
ControlToValidate |
|
Gets or |
|
|
|
|
|
sets the |
|
|
name of |
|
|
the control |
|
|
to |
|
|
validate. |
|
|
|
ControlToCompare |
|
Gets or |
|
|
|
or |
|
sets the |
|
identifier |
|
|
|
|
ValueToCompare |
|
of the |
|
control on |
|
|
|
|
|
|
the Web |
|
|
page to |
|
|
compare |
|
|
with. |
|
|
Gets or |
|
|
sets a |
|
|
specific |
|
|
value that |
|
|
is used to |
|
|
compare |
|
|
against. |
|
|
|
Display |
|
Gets or |
|
|
|
|
|
sets the |
|
|
display |
|
|
appearanc |

Table 6-2: Properties of the CompareValidator control
Property
ErrorMessage
RunAt
Definition
e of the validator control on the Web page.
Gets or sets the error message displayed for this control.
Specifies that this control runs on the server.
A good example for the CompareValidator control would be to compare passwords the first time a user creates an account in your Web application. The following code fragment shows the code for the CompareValidator control:
<!-- Compare Field Validator -->
Password:<br>
<asp:textbox id="Password" maxlength="16" runat="Server"/><br>
Confirm:<br>
<asp:textbox id="Confirm" maxlength="16" runat="Server"/>
<asp:comparevalidator
id="cvPasswords"
controltocompare="Password"
controltovalidate="Confirm"
display="dynamic"
errormessage="Passwords do not match, please try again."
runat="Server">
</asp:comparevalidator>
Launch your browser and navigate to Compare.aspx. Figure 6-5 should be displayed.

Figure 6-5: Compare validator control example 1
Click the Validate button. Why did the CompareValidator control fail to display your error message? Simple; even though you specified the CompareValidator control, you did not specify the RequiredValidator control, meaning that neither Password nor Confirm is a required value. So blank passwords would compare. Keep this in mind when using the CompareValidator control.
This time enter a value for the Password field, press the Tab key, enter a different value for Confirm (for testing purposes), and then press the Tab key again. A screen similar to Figure 6-6 should be displayed, depending on the values that you entered.
Figure 6-6: Compare validator control example 2
This time the validator works as intended and displays the error message that the Password and Confirm TextBox controls do not match.

Now, enter the same values for the Password and Confirm fields and click the Validate button. Because the passwords match, the error message should not be displayed and you should see your message displayed on the page, as in Figure 6-7.
Figure 6-7: Compare validator control example 3
This is just one example of using the CompareValidator control. As shown earlier in Table 6-2, you could specify the ValueToCompare property instead of the ControlToCompare property. Try it! Delete the ControlToCompare property, add the ValueToCompare property, and set its value to some test value. Then run the Compare.aspx sample again. Validation will fail if the value entered in the
ControlToValidate field does not match the value in the ValueToCompare property.
Note This control is case-sensitive. For instance "RICK = Rick" will not compare as being equal.
Using the RangeValidator Control
Use the RangeValidator control to determine whether a value falls within the specified range. It checks whether the value of the associated input control is within some minimum and maximum, which can be a constant value or the value of another control. Table 6-3 lists the properties commonly used with the RangeValidator control.
Table 6-3: Properties of the RangeValidator control
Property
ID
Definition
This property gets or sets the identifier for the control. This identifier is used via programm ing to access the control's properties. If an identifier

Table 6-3: Properties of the RangeValidator control
Property
Definition
does not exist, then you cannot write handlers for this control.
ControlToValidate |
|
Gets or |
|
|
|
|
|
sets the |
|
|
name of |
|
|
the control |
|
|
to |
|
|
validate. |
|
|
|
MaximumValue |
|
Gets or |
|
|
|
|
|
sets the |
|
|
maximum |
|
|
value of |
|
|
the |
|
|
validation |
|
|
range. |
|
|
|
MinimumValue |
|
Gets or |
|
|
|
|
|
sets the |
|
|
minimum |
|
|
value of |
|
|
the |
|
|
validation |
|
|
range. |
|
|
|
MaximumControl |
|
Gets or |
|
|
|
|
|
sets the |
|
|
ID of the |
|
|
control |
|
|
that |
|
|
specifies |
|
|
the |
|
|
maximum |
|
|
value of |
|
|
the |
|
|
validation |
|
|
range. |
|
|
|
MinimumControl |
|
Gets or |
|
|
|
|
|
sets the |
|
|
ID of the |
|
|
control |
|
|
that |
|
|
specifies |
|
|
the |
|
|
minimum |
|
|
value of |
|
|
the |
|
|
validation |
|
|
range. |
|
|
|
Display |
|
Gets or |
|
|

Table 6-3: Properties of the RangeValidator control
Property
Definition
sets the display appearanc e of the validator control on the Web page.
ErrorMessage |
|
Gets or |
|
|
|
|
|
sets the |
|
|
error |
|
|
message |
|
|
displayed |
|
|
for this |
|
|
control. |
|
|
|
Type |
|
Gets or |
|
|
|
|
|
sets the |
|
|
data type |
|
|
to |
|
|
determine |
|
|
how the |
|
|
values |
|
|
should be |
|
|
compared. |
|
|
For |
|
|
instance, |
|
|
string to |
|
|
string or |
|
|
int to int. |
|
|
|
RunAt |
|
Specifies |
|
|
|
|
|
that this |
|
|
control |
|
|
runs on |
|
|
the server. |
Type property
You can set the Type property to one of the following values, which determines the type of values in the range:
§*Currency: The data type is Currency.
§*Date: The data type is DateTime.
§*Double: The data type is Double.
§*Integer: The data type is Integer.
§*String: The data type is String.
MinimumValue and MaximumValue properties
The RangeValidator control is useful when you need to make sure that a value falls within a specified range of values. Take a look at the code fragment that follows for the RangeValidator control:
<!-- Range Validator Control -->
Number of tickets:<br>
<asp:textbox id="Tickets" maxlength="2" columns="2" runat="Server"/>
<asp:rangevalidator