0

I'm trying to change the color of a text in a label per condition.

I have tried the following option but none of them works:

if (gameNameTXT.Text != "")
{
    pubName.ForeColor = System.Drawing.Color.Green;
    pubName.ForeColor = System.Drawing.Color.FromArgb(11, 102, 35);
    pubName.Attributes.CssStyle.Add("color", "green");
}

(pubName in the ID of the label)

I have located the snipped code in the Page_Load section and/or the Page_Init section.

What I want is that the color of the text changes to green if the user wrote something in the text box.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 2
    The short answer is you should do this with javascript. The longer answer will be more complicated - but will ultimately end with "you should do this with javascript". – mjwills Jun 21 '20 at 09:19
  • I hope your code is immersed in an event handler such as public void GameNameTextBox_OnChanged(object sender, EventArgs ev) {...}. Also that your Textbox has an AutoPostback = "true" – LongChalk Jun 21 '20 at 10:29

1 Answers1

0

do you try on onChange event of gameNameTXT?
may be you need set AutoPostBack=true

Son0nline
  • 51
  • 2
  • 5