[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

Override validation controls to apply a default CssClass?

ASP.NET 4 no longer displays validation controls in red, so the hundred or so I've got across my web application now don't show up very well.

Is it possible to reconfigure an application to add a default CSS class to all validation controls?  I did wonder whether you have to override the Render method and do this yourself, but surely there's an easier way?
0
Rouchie
Asked:
Rouchie
  • 4
  • 4
1 Solution
 
richard_hughesCommented:
Hello Rouchie

The easiest way would be to add your own CSS class to each control. Using the !important attribute would help here.

If this is not an option, what is the emitted HTML for the validation control? If there is a common, unique element, you can also do this with jQuery easily in your master page.

Thanks,

Richard
.validationControl
{
    color: red !important;
}

Open in new window

0
 
RouchieAuthor Commented:
Hi Richard

yes I appreciate what you are saying, however, it would mean me manually editing all 100+ controls in my app.  I am looking for a way (if possible) to force ASP.NET to add the class name automatically.

I have successfully enumerated the controls using a Master page, checked their type and then added the CSS class name, but it seems like an awfully painful process, so am looking for something a bit more sophisticated if possible.
0
 
richard_hughesCommented:
It sounds like the best way would be to use jQuery to do this for you. What is the HTML like?

Thanks,

Richard
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RouchieAuthor Commented:
Its different on every page based on that particular page's function within the app -  this is why the solution really needs to be ASP.NET-based and preferably at a low level...!  I know there is a BaseValidator class but have no idea how to override it, or whether even web.config supports the ability to configure validation options...
0
 
richard_hughesCommented:
OK. How about using ASP .NET Themes. These can work with ASP .NET controls directly and require no actual C# code change.

Right click the web application -> Add -> ASP .NET Folder -> Theme ->
Right click the created Theme -> New Item -> New Skin File
Right click the created Theme -> New Item -> New Style Sheet File

Add the following into the skin file:

<asp:MyValidator CssClass="validator" runat="server" />

Add the following into the CSS file:

.validator
{
      color: Red !important;
}

On each page where there is a validator, in the top line of the ASPX page, add a new 'Theme' attribute and select the just created theme.

This method would save looping around controls in the master page.

Thanks,

Richard Hughes
0
 
RouchieAuthor Commented:
Hmm not ideal either I'm afraid because it would mean at least 100 page edits putting in the reference to the Theme.  There must be a way to 'hack' into the validators' render method surely...!?!
0
 
richard_hughesCommented:
There would be, but it would involve changing the ASP .NET tags used in each instance to use your new hacked object.

Is there any common HTML between the validators? If there is, we may be able to avoid lots of editing.
0
 
RouchieAuthor Commented:
After a week's worth of investigation, rather disappointly, it emerges that Themes are the way to achieve this.  However, you can apply Themes to every page using a single line in web.config.  Unfortunately I have some duplicate Theme data as I can't assign multiple Themes to a page, but its not such a big deal.  Thanks for your contribution.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now