Lui.net
Lui.net

Reputation: 81

how to display literal control in specific color

I have a survey style page that I want to setup controls in case a question is not answered I have attached the following code to a Literal. I would like to know if I can change the color of the text inside the literal to Red.

if (RadioButtonList1.SelectedItem == null)
   {
       string showmsg = "Please Answer Question";
       Literal1.Text = showmsg;
   }

Upvotes: 7

Views: 19148

Answers (4)

Garrison Neely
Garrison Neely

Reputation: 3289

I would change it to a Label. At that point, you have the option of using CSS and the .CssClass property, or just straight ASP.NET using the ForeColor property.

Upvotes: 2

TheTechGuy
TheTechGuy

Reputation: 17354

You can use Label control instead which has ForeColor property that you can set

<asp:Label ID="lblMessage" ForeColor="Gren" runat="server" ></asp:Label>

In code behind

lblMessage.ForeColor = System.Drawing.Color.Green;

Upvotes: 1

Habib
Habib

Reputation: 223187

Supply it HTML as,

string showmsg = "<span style='color:red'> Please Answer Question </span>";

That will be rendered as multiple spans. It's better if you use other controls which support color change from the C# code. Like Label and use its property Label.ForeColor

Upvotes: 6

Vikrant
Vikrant

Reputation: 5036

If you don't want to change in Code Behind, do same thing in Design. But, here span is outside Literal tag:

<span style="color:red"><asp:Literal ID="litmessage" runat="server"></asp:Literal></span>

It works too and no need to upload bin files again, if your application is LIVE!

Upvotes: 2

Related Questions