PDA

View Full Version : Remove ForeColor from asp:CustomValidator


johnnyb
10-23-2009, 02:56 PM
Hi,

I'm fairly new to ASP.NET, having come from a PHP background. I'm making a form, and I want to remove the style="color:red" from a asp:CustomValidator control so that I can control the style of my validator from my CSS file, however it defaults to red, and I can't seem to find a way to remove it. Does anyone know how?

Thanks,

John

Mike_O
10-23-2009, 06:23 PM
Hey johnnyb

That's weird. You're right. After trying it myself, the text being wrapped remains red. I tried to remove the style programmatically inside
private void Page_Load(object sender, EventArgs e){...} by saying:
CustomValidator1.Style.Remove("color");
..but that didn't work.However, if I would say
CustomValidator1.Style.Add("color","Green");
..that did change it to green.

So, judging from that, I finally I tried this:
CustomValidator1.Style.Add("color","");
..and then added attribute "...CssClass="MyTestClass"... to the validator control. I defined the class with the new color like you want and it successfully overrode the color.

so, I'd say do either that or just wrap the validator text inside a div or something, for which you define a class.

Regards,
Mike

johnnyb
10-23-2009, 07:24 PM
That would work. It does suck that we're forced to take a route like that.

I found another solution - it's not 100% browser tested yet, (I've just checked FF3.5 & Chrome), to use !important in my CSS file. That seems to override the style declared in the element's style attribute. Also not ideal, but if it works in IE, it'll keep an extra element off of the page.

Mike_O
10-23-2009, 08:19 PM
I've never been a big fan of .NET validation controls, and I never use them in my projects. In my opinion, using them is like reinventing the wheel. There is nothing about them that one cannot mimic simply by writing a few lines of code. Given the following example:
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/validation/requiredfieldvalidator.aspx

... I can do exactly the same thing instead by having a OnClick handler for the button and inside putting:

if(Text1.Text.Trim().Length == 0)
{
//complain
}

So, I say avoid them when you can, but again that's strictly my opinion.

Regards,
Mike