Learner
Learner

Reputation: 71

How to change the colour of a string when binding to a label in asp.net?

I have 2 text boxes in which the user enters values and clicks a button.When the button is clicked I want to replace the value in the first box with to the second box and show it in a label.

I want to show the replaced value in a colour format so that the user can identify it easily.

So how can a string can be colour formatted, I want to colour the string not the label

For eg:

<asp:Label ID="lblName" runat="server"></asp:Label>
string value1 = "Hi! This is a demo code";
lblName.Text = value1.Replace("demo", "sample");

Now my label shows me "Hi! This is a sample code"

There I want only the sample to be coloured and rest be same as a normal label text be

Upvotes: 1

Views: 1862

Answers (2)

HashPsi
HashPsi

Reputation: 1391

A simple way to do it would be to wrap the replacement text in a span with an associated style or css class:

lblName.Text = value1.Replace("demo", "<span style='color: red;'>sample</span>"));

Upvotes: 2

Sgr3005
Sgr3005

Reputation: 94

Assuming you've already got the code to set the label visible, it's contents, and hide the textbox... if you label element looks something like:

<span runat="server" id="label1" />

After populating it you could just:

label1.Style.Value = "color:red;";

OR

label1.Style.Add("color", "red");

Upvotes: -1

Related Questions