2

I have following validator control. How can we set the color of Text of this control from code behind?

Note: Text of the control is * as shown in the code below.

CODE

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"
    Text="*" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />

2 Answers 2

2

Use the ForeColor attribute:

 RequiredFieldValidator1.ForeColor = System.Drawing.Color.Red;
Sign up to request clarification or add additional context in comments.

2 Comments

You just changed your question from CustomValidator to RequiredFieldValidator. And yes I use this code and it works.
No problem. By the way, I recommend you use SetFocusOnError="true" and Display="Dynamic" for more user-friendly result.
1

Something like this should work:

Style myStyle = new Style();
s.BackColor = System.Drawing.Color.Red;

valEmailRecipients.Style = myStyle;

Or simply:

valEmailRecipients.Style.ForeColor= System.Drawing.Color.Red;

See here for reference and here as well.

2 Comments

I am getting error - 'System.Web.UI.CssStyleCollection' does not contain a definition for 'ForeColor'. Also .. Property or indexer 'System.Web.UI.WebControls.WebControl.Style' cannot be assigned to -- it is read only
@Lijo yes, because you changed the Type of control. You had originally posted a very different markup which had a CustomValidator not a RequiredFieldValidator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.