views:

2838

answers:

4

Let's say the page TestPage.aspx has two controls. The first control is an address control that has a validation group called "AddressGroup". This group contains several validation controls which are colated in the validation summary on that control. The second control is a credit card control and has a validation group called "CreditCardGroup". It also has several validators and a summary to display the results. To add to the problem, there are some random controls on the page that also have validators which are tied to a third ValidatorSummary control.

When the user presses the "Do it all" button, I would like the page to trigger all three validation groups. The button itself can be tied to a single group or an unlabeled group. It can not be tied to multiple groups as far as I can tell.

The solution is not to extract the validation from the controls as that would deminish the value of having them in seperate controls. Thanks for your thoughts.

+1  A: 

Call the Validate method for each validation group individually inside the button's click handler:

bool isValidTest = false;
Validate("AddressGroup");
isValidTest = IsValid;
Validate("CreditCardGroup");
isValidTest &= IsValid;
// etc.
if (!isValidTest) return;

The next problem you may encounter is that the ValidationSummary control is linked to a single validation group. The only way that I've found to display all the error messages for multiple groups (without walking the control tree) is use multiple ValidationSummary controls.

With user controls, you may want to have its Validate method perform validation for all the controls it contains and display its own summary.

Edited to add: The isValidTest variable is not needed. According to the docs:

Note that when you call the Validate method, the IsValid property reflects the validity of all groups validated so far.

Jamie Ide
+2  A: 

Are you talking client side or server side validation? Jamie's answer is spot on for server side, but for client side validation you will probably need to write your own JS function that will trigger validation on all three groups in concert.

Goblyn27
For JS, you could call Page_ClientValidate(), this will trigger validation on all groups in page.
Ahmed
+1  A: 

Call Page.Validate() on server side it will validate all the validators..

Basit Tanveer
A: 

How can I assign more than one validation group for same control in asp.net?

Vishnu K B