How to change asp.net Label background color programmatically

Set or change Label background color programmatically
Label is an asp.net web server control. this server control is used to display text on web page. label server control has a built in property to set or change its background color programmatically at run time.

Label server control's BackColor property get or set the background color of label control. the BackColor property value type is System.Drawing.Color. this 'Color' represents the background color of label control. we can simply input any valid color name for BackColor property value such as Magenta, Yellow, Pink etc. we need to import the System.Drawing namespace to apply label background color programmatically.

The following asp.net c# example code demonstrate us how can we apply label background color dynamically at run time in an asp.net application.
LabelBackColor.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Label1.BackColor = System.Drawing.Color.LightGreen;
    }
    protected void Button2_Click(object sender, System.EventArgs e)
    {
        Label1.BackColor = System.Drawing.Color.SkyBlue;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to set, change Label BackColor (background color) programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">Label Example: BackColor</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             Text="This is a test Label"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             ForeColor="Crimson"
             Text="Label Background LightGreen"
             OnClick="Button1_Click"
             Font-Bold="true"
             />
        <asp:Button 
             ID="Button2" 
             runat="server" 
             Font-Bold="true"
             ForeColor="Crimson"
             Text="Label Background SkyBlue"
             OnClick="Button2_Click"
             />
    </div>
    </form>
</body>
</html>
Related asp.net examples