views:

699

answers:

3

Is there a method I can call that retrieves a boolean value of whether or not a particular ValidationGroup is valid? I don't want to actually display the validation message or summary - I just want to know whether it is valid or not.

Something like:

Page.IsValid("MyValidationGroup")
+1  A: 

Try this:

Page.Validate("MyValidationGroup");
if (Page.IsValid) 
{
    //Continue with your logic
}
else
{
    //Display errors, hide controls, etc.
}

Not exactly what you want, but hopefully close.

Matthew Jones
So will Page.Validate cause my ValidationSummary to display?
Mike C.
You would need to programmatically disable/hide the validation summary if you want to use this solution in that manner.
Matthew Jones
+2  A: 

Have you tried using the Page.Validate(string) method? Based on the documentation, it looks like it may be what you want.

Page.Validate("MyValidationGroup");
if (Page.IsValid)
{
    // your code here.
}

Note that the validators on the control that also caused the postback will also fire. Snip from the MSDN article...

The Validate method validates the specified validation group. After calling the Validate method on a validation group, the IsValid method will return true only if both the specified validation group and the validation group of the control that caused the page to be posted to the server are valid.

Scott Ivey
+1  A: 
protected bool IsGroupValid(string sValidationGroup)
{
   foreach (BaseValidator validator in Page.Validators)
   {
      if (validator.ValidationGroup == sValidationGroup)
      {
         bool fValid = validator.IsValid;
         if (fValid)
         {
            validator.Validate();
            fValid = validator.IsValid;
            validator.IsValid = true;
         }
         if (!fValid)
            return false;
      }

   }
   return true;
}
Pavel Chuchuva