YouTip LogoYouTip

Prop Webcontrol Standard Backcolor

## ASP.NET BackColor Property The `BackColor` property is a standard property shared by many ASP.NET Web server controls. It is used to get or set the background color of a specified control. --- ## Definition and Usage The `BackColor` property defines the background color of a Web server control. This property accepts any valid HTML color value, including predefined color names (e.g., `Red`, `Blue`, `LightGray`) and hexadecimal color codes (e.g., `#FF0000`, `#E0FFFF`). --- ## Syntax ```xml ``` ### Property Values | Value | Description | | :--- | :--- | | `color` | Specifies the background color of the control. Must be a valid (https://www.w3schools.com/colors/colors_names.asp). | --- ## Code Examples ### Example 1: Setting the Background Color in Markup The following example demonstrates how to set the background color of an ASP.NET Button control to light cyan (`#E0FFFF`) directly in the `.aspx` markup: ```xml
``` ### Example 2: Setting the Background Color Programmatically (Code-Behind) You can also set or change the `BackColor` property dynamically in your C# or VB.NET code-behind file using the `System.Drawing.Color` structure. **C# Code-Behind:** ```csharp protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Setting the background color using a predefined Color structure button1.BackColor = System.Drawing.Color.LightCyan; // Alternatively, setting the background color using an HTML hex code // button1.BackColor = System.Drawing.ColorTranslator.FromHtml("#E0FFFF"); } } ``` --- ## Considerations and Best Practices 1. **CSS Precedence:** If you define a background color for a control using both the `BackColor` property and a CSS class (via the `CssClass` property), the CSS styles or inline styles may override the `BackColor` property depending on how the browser renders and prioritizes the styles. 2. **Color Accessibility:** When setting a custom `BackColor`, ensure there is sufficient contrast between the background color and the foreground text color (`ForeColor`) to maintain readability and comply with web accessibility standards (WCAG). 3. **Browser Compatibility:** The `BackColor` property is rendered as an inline CSS style (`background-color: color;`) in the output HTML, making it highly compatible across all modern web browsers.
← Prop Webcontrol Standard BordeProp Webcontrol Standard Acces β†’