Validation Groups

A cool feature of the validation controls in .NET is the property “ValidationGroup”. With this property, you can easily activate different validation controls based on which button is clicked.

For example, suppose you have a content page that has a search box. In addition, this page has a feedback form at the bottom of the page where you can rate “Was this page useful” from 1 to 5. The requirement for this page is that if you click on the search button, you must have entered a search term, and if you click on the rate page button, you must have selected a numerical rating.

To do this, you would add a required field validator for each of these of these fields. However when you test this page, you discover that when you click on the search button, it complains that you didn’t select a page rating, and when you click to page rating button it complains that you didn’t enter a search term. How do you get around this?

Use the ValidationGroup property. In the validation control for the search term text box and the search button, set the property ValidationGroup =”search”, and in the validation control for the page rating entry field and the page rating button set the property ValidationGroup=”pagerating”. This way, when you click the search button, it only runs the validation controls that have ValidationGroup=”search”, and it will not complain that there is not a page rating entered. Likewise with the page rating button.

Advertisements
%d bloggers like this: