Solved

Override validation controls to apply a default CssClass?

Posted on 2011-09-30
8
360 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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