My Technical Notes

Wednesday, 5 June 2013

Forcing a Validation Error to appear using PreRender event

Given the following method which adds a validation error message to the screen:


public void AddValidationError(string message, string validationGroup = null)
{
    this.Page.Validators.Add(new CustomValidator()
    {
        ErrorMessage = message,
        IsValid = false,
        ValidationGroup = validationGroup
    });
}

I was perplexed when a call to it did not result in the ValidationSummary displaying the error. What I ended up doing was to tap into the PreRender event on the page to force the validation message to appear:


this.PreRender += delegate(object s, EventArgs e1)
{
    AddValidationError("you are shown a validation error");
};

No comments: