YouTip LogoYouTip

Prop Webcontrol Standard Font

## ASP.NET Web Control Font Property The `Font` property is a standard property applied to ASP.NET Web server controls. It allows developers to define, customize, and manipulate the typography and text styling of a control. --- ## Definition and Usage The `Font` property is used to set or return the font properties of a Web server control. It acts as a wrapper object that contains several subproperties (such as bold, italic, size, and typeface name) to control the visual appearance of text within the rendered HTML. --- ## Syntax You can set the font properties declaratively in the `.aspx` markup using the hyphenated syntax (`Font-Subproperty`): ```html ``` Alternatively, you can access and modify these properties programmatically in your code-behind (C# or VB.NET): ```csharp // C# Code-behind example Button1.Font.Name = "Verdana"; Button1.Font.Size = FontUnit.Point(12); Button1.Font.Bold = true; ``` --- ## Font Subproperties The `Font` property is an instance of the `FontInfo` class. It exposes the following subproperties: | Subproperty | Description | Accepted Values | | :--- | :--- | :--- | | **Bold** | Determines whether the text is rendered in bold. | `true` \| `false` | | **Italic** | Determines whether the text is rendered in italics. | `true` \| `false` | | **Name** | Specifies the primary font family name (e.g., "Verdana" or "Arial"). Setting this property automatically updates the `Names` property with a single-element array containing this value. | String (e.g., `"Arial"`) | | **Names** | An ordered array of font family names. This is useful for specifying fallback fonts. Setting this property automatically updates the `Name` property with the first element of the array. | String array (e.g., `new string[] {"Verdana", "Arial", "sans-serif"}`) | | **Strikeout** | Determines whether the text has a line through it (strikethrough). | `true` \| `false` | | **Underline** | Determines whether the text is underlined. | `true` \| `false` | | **Size** | Specifies the size of the font. | A `FontUnit` value (e.g., `"15"`, `"Small"`, `"12pt"`, `"1.2em"`) | --- ## Code Examples ### Declarative Markup Example The following example demonstrates how to set the font properties of an ASP.NET `Button` control directly in the `.aspx` file: ```html
``` ### Programmatic Code-Behind Example (C#) You can also dynamically change the font properties at runtime based on user interaction or business logic: ```csharp protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Apply multiple font styles programmatically Label1.Text = "Welcome to YouTip!"; Label1.Font.Name = "Segoe UI"; Label1.Font.Size = FontUnit.Parse("14px"); Label1.Font.Italic = true; } } ``` --- ## Developer Considerations 1. **CSS vs. Inline Font Properties**: When you use the `Font` property in ASP.NET, the framework renders these styles as inline CSS styles (e.g., `style="font-family:Verdana;font-size:15px;"`) on the resulting HTML element. For modern web development, it is often recommended to manage typography via external CSS classes using the `CssClass` property to keep your markup clean and maintainable. 2. **Font Fallbacks**: Always consider using the `Names` property instead of a single `Name` when you want to ensure cross-platform compatibility. For example, setting `Font-Names="Helvetica, Arial, sans-serif"` ensures that if the client machine does not have Helvetica installed, it will fall back to Arial or a generic sans-serif font. 3. **FontUnit Flexibility**: The `Font-Size` property accepts various units of measurement, including absolute sizes (`Small`, `Medium`, `Large`), relative sizes (`Smaller`, `Larger`), or specific units (`px`, `em`, `pt`, `%`).
← Prop Webcontrol Standard ForecProp Webcontrol Standard Enabl β†’