YouTip LogoYouTip

Prop Webcontrol Style Forecolor

## ASP.NET WebControl ForeColor Property The `ForeColor` property is a member of the `System.Web.UI.WebControls.Style` class. It is used to get or set the foreground color (which typically represents the text color) of an ASP.NET Web server control. --- ## Definition and Usage The `ForeColor` property defines the color of the text or foreground elements within a Web control. When rendered to the client browser, this property is translated into the CSS `color` style attribute. --- ## Syntax ### Declarative Syntax ```asp ``` ### Programmatic Syntax (C#) ```csharp controlID.ForeColor = System.Drawing.Color.FromName("color"); // Or using ColorTranslator controlID.ForeColor = System.Drawing.ColorTranslator.FromHtml("#HexColor"); ``` ### Property Values | Value | Description | | :--- | :--- | | `color` | Specifies the foreground color of the control. This must be a valid HTML color representation (such as a color name, a hexadecimal value, or an RGB value). | --- ## Code Examples ### Example 1: Declarative Markup (ASPX) The following example demonstrates how to set the foreground color of an ASP.NET Button control to red using a hexadecimal color code in the markup. ```html
``` ### Example 2: Programmatic Control (C# Behind-the-Code) You can also dynamically change the `ForeColor` of a control in your code-behind file based on user interaction or business logic. **ASPX Markup:** ```html
``` **C# Code-Behind:** ```csharp protected void Button1_Click(object sender, EventArgs e) { // Setting the ForeColor using a predefined Color structure Label1.ForeColor = System.Drawing.Color.Green; // Alternatively, setting the ForeColor using a Hexadecimal value // Label1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#00FF00"); } ``` --- ## Technical Considerations 1. **CSS Translation:** When ASP.NET renders the control to the browser, the `ForeColor` property is compiled into an inline CSS style. For example, `ForeColor="Red"` renders as `style="color:Red;"` in the HTML tag. 2. **Color Formats:** The property accepts standard HTML color formats: * **Color Names:** `Red`, `Blue`, `DarkGray` * **Hexadecimal Values:** `#FF0000`, `#0000FF` * **RGB/RGBA Values:** `rgb(255, 0, 0)` 3. **Inheritance:** If a parent control (like a `Panel` or `PlaceHolder`) has its `ForeColor` set, child controls will typically inherit this foreground color unless they explicitly override it with their own `ForeColor` property.
← Prop Webcontrol Style HeightProp Webcontrol Style Font β†’