Solved

Override validation controls to apply a default CssClass?

Posted on 2011-09-30
8
357 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 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