β’ **Color Names** (e.g., `Red`, `Blue`, `LightGray`)
β’ **Hexadecimal values** (e.g., `#FF0000`, `#FFCC80`)
β’ **RGB/RGBA values** (e.g., `rgb(255, 0, 0)`) | --- ## Code Examples ### Example 1: Declarative Markup The following example demonstrates how to set the background color of an ASP.NET Button control directly in the `.aspx` markup using a hexadecimal color code: ```xml ``` ### Example 2: Programmatic Implementation (C#) You can also dynamically change the background color of a control in the code-behind file based on user interaction or application logic. **ASPX Markup:** ```xml ``` **Code-Behind (C#):** ```csharp using System; using System.Drawing; protected void btnSuccess_Click(object sender, EventArgs e) { // Setting color using System.Drawing.Color predefined names Label1.BackColor = Color.LightGreen; Label1.Text = "Operation completed successfully!"; } protected void btnWarning_Click(object sender, EventArgs e) { // Setting color using a Hexadecimal value Label1.BackColor = ColorTranslator.FromHtml("#FFCDD2"); Label1.Text = "An error has occurred."; } ``` --- ## Considerations & Best Practices 1. **CSS Classes vs. Inline Styles**: While setting `BackColor` directly on the control is convenient, it renders as an inline style (`style="background-color: ..."`). For larger production applications, it is generally recommended to manage colors via external CSS classes using the `CssClass` property to maintain a clean separation of concerns. 2. **Color Structure**: In the code-behind, the `BackColor` property accepts a `System.Drawing.Color` structure. You cannot assign a raw string directly to it in C#; you must use helper methods like `Color.FromName()` or `ColorTranslator.FromHtml()`. 3. **Accessibility (Contrast Ratio)**: When changing the `BackColor` of a control, ensure that the foreground text color (`ForeColor`) maintains a high enough contrast ratio to remain readable and compliant with WCAG accessibility guidelines.
YouTip