Use WebControlAdapter to change the way of how CustomValidator is rendered. You will want to override RenderBeginTag and RenderEndTag methods to render your div around the default span.
public class CustomValidatorAdapter : WebControlAdapter
{
protected override void RenderBeginTag(HtmlTextWriter writer)
{
writer.RenderBeginTag("div");
base.RenderBeginTag(writer);
}
protected override void RenderEndTag(HtmlTextWriter writer)
{
base.RenderEndTag(writer);
writer.RenderEndTag(); // this will close div
}
}
To connect this adapter to you application you have two options. First is to do it programmatically in page constructor:
var adapters = Context.Request.Browser.Adapters;
var customValidatorType = typeof(CustomValidator).AssemblyQualifiedName;
var adapterType = typeof(CustomValidatorAdapter).AssemblyQualifiedName;
if (!adapters.Contains(customValidatorType))
{
adapters.Add(customValidatorType, adapterType);
}
Second option is to use .browser file:
Add new Browser File to your project. It will reside in App_Browsers folder. Then replace contents with following declaration:
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.CustomValidator"
adapterType="<your namespace>.CustomValidatorAdapter" />
</controlAdapters>
</browser>
</browsers>
CssClass) to the validator. Also, RegularExpressionValidator is good for email.CssClass, but I need instead<span>usedivand other html elements.