views:

381

answers:

1

Is there a clean and centralized way to automatically designate a background color or other CSS property for ASP.Net controls (i.e. a TextBox) with an enabled RequiredFieldValidator?

Currently I have manually set the background color of required controls to yellow. I would like to replace that with a central method so if the client requests a different color or marker or if a field's status changes from required to not or vice versa, I won't miss any of the controls.

Thanks

Update

This site is pre-compiled. Can something append a Css Class or other standards-compliant flag to items in the ControlToValidtate property at compile time?

+2  A: 

Why not add a css class of required. You can have multiple classes on a single control by space delimiting them so imagine an input field which takes a number:

Now I can have two classes one which right aligns the text and one which handles the required field requirements.

Edit

One option before the page is rendered to walk through each control in the page, and if it's a required field validator, then find it's corresponding control and set the css property; however, this is a lot of work for somethign which you can tackle at design time.

JoshBerke
Because unfortunately the validators don't render any trace by which to identify those elements.
BC
Yes but you know at design time that you added a required field validator.
JoshBerke
Exactly. I've even taken this to the next level in one of my projects and dynamically append validation controls based on the classes in the CssClass attribute (e.g. if the CssClass="required email", I'll append a required field validator and regex validator dynamically)
Chris Pebble
Ohh now that is interesting the inverse of my edit...neat idea
JoshBerke
@Chris - so you are going the other way around then? Adding <asp:RequiredFieldValidator ... /> to items that have class="required" in them?
Rob Allen
Sounds like he is but after thinking about this, if you want different validation messages for example then this doesn't make sense.
JoshBerke