Carisle
Carisle

Reputation: 467

How To Change The Color of Text In Asp.net Code Behind

Hello everyone I am newbie using asp.net I want to ask on how to change color of the label with some conditional statement. For e.g if the stock is below 20 then the label will became red if not then it's black. Please help I need solution for this, thanks everyone.

Upvotes: 0

Views: 22108

Answers (1)

competent_tech
competent_tech

Reputation: 44931

You have a couple of choices: you can use the builtin attributes on the label or you can use CSS classes.

I prefer to use CSS since it provides much more flexibility and doesn't encode visual decisions into the application (meaning they can be adjust by the end user or a visual designer as needed).

Here is the property approach:

    If StockPrice < 20 Then
        lblStockPrice.ForeColor = System.Drawing.Color.Red
    Else
        lblStockPrice.ForeColor = System.Drawing.Color.Black
    End If

and here is the css approach:

CSS:

.NormalStockPrice 
{
    color: Black;
}

.WarningStockPrice
{
    color: Red;
}

Code:

    If StockPrice < 20 Then
        lblStockPrice.CssClass = "WarningStockPrice"
    Else
        lblStockPrice.CssClass = "NormalStockPrice"
    End If

Upvotes: 3

Related Questions