Search code examples
c#asp.netwebformscustom-controlscustom-validators

How to bubble up validation error from child control to parent control


My asp.net webform page has a user control (Control1.ascx) and a Save button. Control1 contains another user control Control2.ascx that contains several custom validators.

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
 if (somecheck1)
     args.IsValid = false;
 else
     args.IsValid = true;
}

protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
{
 if (somecheck2)
     args.IsValid = false;
 else
     args.IsValid = true;
}

I don’t want Control1.Save code to execute if any of the validators on Control2 fails. How can this be accomplished? I am thinking about adding a Valid property on Control2 and check for that in the Save button code. Does that reasonable approach? Also what should be done in the Valid property?

EDIT: Thanks for the response but I am not been able to work this out. I have posted a different question with more specific code examples at Handling of validation errors from child controls


Solution

  • Do a page.validate("customValidator1"); where customValidator1 is your validation group. Then fire a Page.IsValid();If the first group validation fails then Page.IsValid() will return false. Implement this in your logic.