Solved

Override validation controls to apply a default CssClass?

Posted on 2011-09-30
8
351 Views
Last Modified: 2012-05-12
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
Comment
Question by:Rouchie
  • 4
  • 4
8 Comments
 
LVL 9

Expert Comment

by:richard_hughes
ID: 36890771
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
 
LVL 25

Author Comment

by:Rouchie
ID: 36890889
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
 
LVL 9

Expert Comment

by:richard_hughes
ID: 36890894
It sounds like the best way would be to use jQuery to do this for you. What is the HTML like?

Thanks,

Richard
0
 
LVL 25

Author Comment

by:Rouchie
ID: 36890910
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Accepted Solution

by:
richard_hughes earned 500 total points
ID: 36890940
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
 
LVL 25

Author Comment

by:Rouchie
ID: 36892289
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
 
LVL 9

Expert Comment

by:richard_hughes
ID: 36897934
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
 
LVL 25

Author Closing Comment

by:Rouchie
ID: 36922859
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now