Whenever I find this situation, this is what I do:
var val = new CustomValidator() { ErrorMessage = "This is my error message.", Display = ValidatorDisplay.None, IsValid = false, ValidationGroup = vGroup }; val.ServerValidate += (object source, ServerValidateEventArgs args) => { args.IsValid = false; }; Page.Validators.Add(val);
And in my ASPX code, I have a ValidationSummary control with a ValidationGroup parameter with the same value as vGroup .
Then, after I have loaded as many CustomValidators (or any other types of validators) with codebehind as I want, I just call
Page.Validate() if (Page.IsValid) {
Calling the Page.Validate() method calls the lambda-related method of all inserted code lock validators, and if any result returns false, the page is invalid and returns without executing code. Otherwise, the page returns a valid value and executes a valid code.
source share