Q:

How do I pass any validation control to a method?

I am creating a webform using ASP.NET and C#.  I created a method that posts an error message which is picked up by the Validation Summary control.  
protected void PostErrorToCusVal(ref System.Web.UI.WebControls.CustomValidator ErrorObj, string ErrHead, string ErrMsg) {
  ErrorObj.ErrorMessage = "*SomeHTML/CSS*" + ErrHead + "*SomeHTML/CSS*" + ErrMsg + "*SomeHTML/CSS*";
}

I am having trouble passing anything other than a CustomValidator control to it.  I want to be able to pass any validation control to it and set the ErrorMessage property.  I tried using using BaseValidator and casting, but that wouldn't work.  Is it possible for this to be done?
Thanks,
Ozzy

A:

You can alternatively write a helper method as follows which will be more generic and could be used at many places
        public static void AddExceptionMessageAsValidator(Page page, Exception ex, string message, string validationGroup)
    {
        CustomValidator exValidator = new CustomValidator();
        exValidator.IsValid = false;
        if (message != null)
            exValidator.ErrorMessage = message;
        else
            exValidator.ErrorMessage = ex.Message;

        exValidator.Text = String.Empty;

        if (!String.IsNullOrEmpty(validationGroup))
            exValidator.ValidationGroup = validationGroup;

        page.Validators.Add(exValidator);
    }

For using it, just pass the Page, Exception, Message if any to override the exception message, and the validationgroup of the page.
Hope this helps

