Search code examples
.netcustomvalidator

CustomValidator - ErrorMessage property without Text property issue


I have a requirement that calls for a custom validator. The users want a red asterisk next to the field being validated. The asterisk should always show - indicating that the field is required (depending on a checkbox being checked/unchecked). The error message that shows in the validation summary is rather long. I used a label for the asterisk as the controls are in a .ascx user control that is used on several pages and sometimes the asterisk should show and other times it should not. Because I don't want the error message to show next to the field and I don't want two asterisks, I tried not setting the Text property of the Custom Validator. However, this caused the error message to display where the text would show. I also tried setting the text property to String.Empty through code, but didn't work either. I can't find anything on SO, Google, or MSDN.

The only solution I came up with was to set the text property to an asterisk and set the forecolor to white, effectively making it invisible.

Is there a way to accomplish what I want to do without this workaround?

Thanks in advance!


Solution

  • Set validator's ErrorMessage property, leave Text property empty and set Display property to "None"