Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
142
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

RangeValidator

LessThanEqual, or DataTypeCheck, which we’ll look at shortly. Next, we tell the CompareValidator control to compare the two values by setting the Type property to Integer, which will cause the CompareValidator to treat the values as whole numbers (this property can also be set to Currency or Date, among other options). Finally, we use the ValueToCompare property to make sure that the user’s age is greater than 15. If you load this page in your web browser now, you’ll see that the form is only validated when the user enters an age of 16 or more.

We can also use the CompareValidator control to perform data type checks. To see how this works, let’s replace the age TextBox control with a date-of-birth text box, whose value must be a valid date:

File: Login.aspx (excerpt)

<!-- Birth Date --> <p>

Birth Date:<br />

<asp:TextBox id="birthDateTextBox" runat="server" /> <asp:CompareValidator id="birthDateCheck" runat="server"

Operator="DataTypeCheck" Type="Date"

ControlToValidate="birthDateTextBox"

ErrorMessage="You must enter the date in a valid format!" SetFocusOnError="True" Display="Dynamic" />

</p>

As you can see, the Operator property of the CompareValidator control is set to perform a DataTypeCheck, and the Type property is set to Date. Load the page, and you’ll see that you can’t enter anything other than a valid date into this field. The constituents of a “valid” date will depend on the regional settings on your web server.

RangeValidator

The RangeValidator control checks whether the value of a form field falls between minimum and maximum values. For instance, we could make sure that users who visit our web site were born in a certain decade. If they enter values that don’t fit into the range we specify, the validator will return an “invalid” message.

Let’s continue by expanding Login.aspx even further:

File: Login.aspx (excerpt)

<!-- Birth Date --> <p>

Birth Date:<br />

233

Chapter 6: Using the Validation Controls

<asp:TextBox id="birthDateTextBox" runat="server" />

<asp:RangeValidator id="birthDateRangeTest" runat="server" Type="Date" ControlToValidate="birthDateTextBox" MinimumValue="1/1/1970" MaximumValue="12/31/1979" ErrorMessage="You must've been born in the 1970s to use this web site!" />

</p>

Take Care when Specifying Dates

If you’re outside of the US, you may need to modify the above example. In the US, dates are specified in month-day-year format. In the UK and Australia, they’re specified in day-month-year order, and in other countries, the year is specified first. The ASP.NET runtime will be expecting you to specify dates in your local format, so adjust the values of the MinimumValue and MaximumValue properties accordingly.

Here, we’ve added a RangeValidator to validate the birthDateTextBox control. Our RangeValidator control checks whether the date entered falls within the 1970s, and shows an error message similar to Figure 6.6 if it doesn’t.

Figure 6.6. Using the RangeValidator control

Note that the Type property of the RangeValidator control specifies the data type that’s expected in the control with which it’s associated: if some other data type is entered into this field, it fails validation. As such, we've removed the CompareValidator we added for this purpose.

234

ValidationSummary

ValidationSummary

Imagine we have a form that contains many form fields. If that page contains errors, it could be difficult for users to figure out which control caused a given error, because the page is so big. The ValidationSummary control can alleviate this problem by presenting the user with a list of error messages in one place on the page. Let’s see the ValidationSummary control in use. Add it to the end of your Login.aspx file, like so:

File: Login.aspx (excerpt)

<!-- Submit Button --> <p>

<asp:Button id="submitButton" runat="server" Text="Submit" OnClick="submitButton_Click" />

</p>

<!-- Validation Summary --> <p>

<asp:ValidationSummary id="vSummary" runat="server" /> </p>

When the user clicks the Submit button, the ValidationSummary is populated automatically with a list of all the errors on the page, as we can see in Figure 6.7.

Figure 6.7. Using the ValidationSummary control

235