Monday, March 02, 2009 #

Must use !important on your CSS class for Text color to work on ASP.NET Validation Controls

Technorati Tags: ,

There is absolutely no excuse not to use CSS classes in your asp.net controls.  And just because some appear not to work, doesn’t mean they can’t.

For example, it’s important to note that if you specify a CssClass in an ASP.NET validation control, you will find out that if you specify a color, that it doesn’t take.  That’s because you need to decorate it with !important in your CSS class:

<asp:RegularExpressionValidator id="vldEmailRegex" CssClass="required" . . .

.required
{
    color:  #801517 !important;
    vertical-align: top;
}

Using properties such as ForeColor and others in ASP.NET controls is just wrong for obviously many reasons.   It’s essentially the same thing as an inline style and you’re repeating yourself throughout your controls which completely throws away the benefits of a style sheet in terms of management and debugging your styles throughout your application.  And you might as well throw inline styles in the group with Spaghetti code.  You should never take the “easy way out” and hack your application with styles like this.

Use classes, not inline styles.  That means you need to find out why CssClass isn’t working for some controls because more often then not, you’re not doing something right or there is a simple CSS property that you can use to make it work in your ASP.NET controls via the class you specify.

I wish Microsoft never put in those style properties for any of their web controls;  It completely goes against industry wide standards of using proper CSS (classes, elements, IDs). 

We’re in 2009 people, start using Classes & IDs in CSS…don’t be a spaghetti coder.


posted @ Monday, March 02, 2009 11:21 PM | Feedback (1)