Last post Oct 03, 2008 09:02 AM by charalambos
Apr 03, 2007 01:52 PM|BenCh1|LINK
One of the thing that infuriates me about ASP .NET is that Microsoft has made it so difficult to get rid of the inline styles in the validator controls.
On every one, there is the style declaration: style="color:red;visibility:hidden".
I object to this because I like to control the way my page looks from an external CSS sheet, separating content from presentation as every developer knows he should. I don't really think it's any of ASP .NET's business what colour my Controls are displayed
in - I believe it should be used as a medium to create and manage content, not to make stuff pretty. The other thing I find annoying is that given the choice, I'd rather specify that validators used the CSS "display: none" rule, rather than "visiblilty: invisible"
This is because they still take up space on the page when they're invisible, and make my forms look unnaturally spaced out. A Control with 3 sorts of validator below it has a huge gap below it but a Control with just one validator has a smaller gap.
So, I've been trying for ages to use the Control Adapters to prevent the ASP .NET engine from rendering the inline styles. My ideal scenario would be to give the BaseValidator two additional properties. One would be the CssClass to use when the Control is
valid. This would probably (but wouldn't have to) be a rule like "display: none;" . The other would be a different CssClass to use when the Control is invalid. This would probably (but wouldn't have to) be a rule like "display: inline; color: red". Then, the
I'm sad to say that so far I have failed.
It turns out that it is a much harder thing to accomplish than adapting the other ASP .NET controls. This is mainly because all the properties and scripts that you need are buried deep in the .NET framework, many of them not being exposed at the same level
that I'm developing. I've gone round and around in circles and it's almost as though Microsoft doesn't want me to develop their validation controls. What I want to achieve is so simple, but doing it seems near impossible.
Has anybody else tried? Has anyone had any success? Does anybody else agree with me? Are there any plans to redesign the validators so that they are friendlier to web control adapters in the next release of .NET?
Any opinions, stories (of success or of woe) or suggestions are welcome.
asp .net control adapters validator controls validation style css color visibility display
Sep 17, 2007 12:10 PM|nicjohnson|LINK
I'm in the same boat. Late to the party, but having all of the same troubles. (Dang you, inline styles!!!)
Sep 23, 2007 12:11 AM|neps|LINK
I hate those damn inline style too!
Isn't anyone from the ASP.net team aware of this? There *MUST* be a workaround for this, like a control adapter or something...
Oct 01, 2007 02:30 PM|gbdarren|LINK
I just found out you can override the "color:red" by using this code in the code-behind:
ValidationControl.ForeColor = System.Drawing.Color.Empty
I know it mixes code and style, but it's the best I've found so far if you have to work with the ASP validation controls. If you can, I would use Peter Blum's validation control library. It's more robust than Microsoft's:
Oct 01, 2007 02:33 PM|gbdarren|LINK
Also, you can get the control to use the CSS: "display:none" by setting the Display property of the validation control to "Dynamic":
<asp:RegularExpressionValidator ID="valTxtIncome" ControlToValidate="txtIncome"
ErrorMessage="Invalid Number" Display="Dynamic" CssClass="errorMsg"
validation validator color display css
Feb 15, 2008 03:54 PM|drgonjo|LINK
You can actually override the inline red color rule in an external CSS using !important like this:
color: #666666 !important;
I do agree that you should be able to define two separate CssClasses for the controls though. Currently I haven't found away to get the validation message to display as a block level element without adding another child element to the span which gets generated.
Oct 03, 2008 09:02 AM|charalambos|LINK
All of the above mentioned can be achieved with different attributes. You must set them for each validator control though.
Set CSS class
Remove inline font color
display:none instead of visibility:hidden