Solved

Override validation controls to apply a default CssClass?

Posted on 2011-09-30
8
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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