Exercise: Validation controls - simple

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

The answer to the exercise will be included and explained if you attend the course listed below!

Category ==> ASP.NET webforms - C#  (25 exercises)
Topic ==> Validation controls  (2 exercises)
Level ==> Relatively easy
Course ==> ASP.NET webforms - C#
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Create a new web site, and add the files in the above folder into it.

Try browsing the form frmFilmEntry.aspx.  You should find it does something sensible if you enter a film name and rating, but crashes if you don't:

ASP.NET 4.0 - C# exercise - Validation controls (image 1)

If the film name is blank and/or the rating isn't a number, we have problems

Add validation controls to the form to make it more robust.  Here's a suggestion:

  • A RequiredFieldValidator to check that a film is filled in
  • Another RequiredFieldValidator to check a rating is filled in
  • A CompareValidator to check the rating is an integer
  • A ValidationSummary to display all of the errors found

You can use the .valSummary and .valError classes to style your validation controls, if you like - or create your own.

Here's what the form might look like if you leave the film name blank and enter a rating of Not a number:

ASP.NET 4.0 - C# exercise - Validation controls (image 2)

The two validation errors are listed in the summary at the top of the form

Test that your validation controls work properly, then save and close all of your files.

This page has 0 threads Add post