YouTip LogoYouTip

Prop Webcontrol Style Font

## ASP.NET WebControl Font Property The `Font` property is a sub-property of the `Style` object in ASP.NET Web Forms. It is used to set or retrieve the font properties of a Web server control. --- ## Definition and Usage The `Font` property allows developers to customize the typography of a control, including its typeface, size, weight, and style. In markup, this property is typically accessed using hyphenated sub-properties (e.g., `Font-Name`, `Font-Bold`). --- ## Syntax You can declare font properties inline within an ASP.NET Web control using the following syntax: ```html ``` Alternatively, you can access and modify these properties programmatically in your code-behind (C# or VB.NET): ```csharp // C# Example Button1.Font.Name = "Verdana"; Button1.Font.Size = FontUnit.Point(12); Button1.Font.Bold = true; ``` --- ## Font Sub-properties The `Font` property contains several sub-properties that allow you to control specific aspects of the text appearance: | Sub-property | Description | | :--- | :--- | | **Bold** | Determines whether the font is bold. Possible values are `true` or `false`. | | **Italic** | Determines whether the font is italicized. Possible values are `true` or `false`. | | **Name** | Specifies the primary font family name (e.g., `"Verdana"`, `"Arial"`, or `"Times New Roman"`). Setting this property automatically updates the `Names` property with a single-element array containing this value. | | **Names** | An array of font family names ordered by preference. Setting this property automatically updates the `Name` property with the first element of the array. | | **Strikeout** | Determines whether the font has a strike-through line. Possible values are `true` or `false`. | | **Underline** | Determines whether the font is underlined. Possible values are `true` or `false`. | | **Size** | Specifies the size of the font. This can be set using predefined sizes (e.g., `Small`, `Medium`, `Large`) or specific units (e.g., `12pt`, `1.2em`, `15px`). | --- ## Code Examples ### Example 1: Declarative Markup (ASPX) The following example demonstrates how to set the font properties of an ASP.NET `Button` control directly in the markup: ```html
``` ### Example 2: Programmatic Control (C# Code-Behind) You can also manipulate the font properties dynamically at runtime based on user interaction or application state: ```csharp protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Set font family using a fallback list Button1.Font.Names = new string[] { "Segoe UI", "Arial", "sans-serif" }; // Set font size using FontUnit Button1.Font.Size = FontUnit.Parse("14px"); // Apply styles Button1.Font.Italic = true; Button1.Font.Underline = false; } } ``` --- ## Key Considerations 1. **Font Fallbacks (`Names` vs `Name`):** It is highly recommended to use the `Names` property to specify a list of fallback fonts. If a client's browser does not support the first font, it will gracefully fall back to the subsequent fonts in the list. 2. **Font Size Units:** When setting `Font-Size` in markup, you can use absolute sizes (like `Small`, `Medium`, `Large`) or relative/exact units (like `px`, `em`, `pt`). For modern responsive web design, relative units like `em` or `rem` are preferred. 3. **CSS Classes vs. Inline Styles:** While setting font properties directly on WebControls is convenient, for larger applications, it is best practice to manage typography using external CSS classes and assign them to controls using the `CssClass` property.
← Prop Webcontrol Style ForecoloProp Webcontrol Style Cssclass β†’