How not to show BaseValidator.Text when using ValidationSummary in ASP.NET
I am using a bunch of different asp.net validation validation elements in a web form. Some of them have a Text property set to things like "* Error" or "You missed these fields".
However, some CustomValidator controls have empty Text properties. I left them blank because I am adding the ErrorMessage dynamically, depending on which case fails. (The CustomValidator can have many different conditions, which I set args.IsValid = false)
When an error occurs, the ErrorMessage property that I set is displayed both in the ValidationSummary and inside the Validator element. I do not want it. I want to be able to just show the ErrorMessage in the ValidationSummary and not the BaseValidator.Text property.
My first attempt was to set the Text property to "" space. It didn't work.
What I have implemented (for now) is a period that appears as the same background color. It's a hack - and I don't like it. Damn, maybe that's why I'm here!
Here's the code:
<asp:CustomValidator ID="StackOverflowValidator" runat="server"
Text="."
CssClass="validatorstyle"
Display="Dynamic"
OnServerValidate="validate_AllowedToDoSomething"
ValidationGroup="MainGroup" />
<asp:ValidationSummary ID="mainGroupValidationSummary" runat="server"
ValidationGroup="MainGroup"
DisplayMode="BulletList"
HeaderText="There was an error in saving. Please check the following:" />
Inside validate_AllowedToDoSomething I call:
StackOverflowValidator.ErrorMessage = "Custom Error Message #1";
args.IsValid = false;
return;
What I get is "User error # 1" twice on the web form. Thanks in advance!
source to share