Export (0) Print
Expand All

WebControl.Font Property

Gets the font properties associated with the Web server control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public virtual FontInfo Font { get; }

Property Value

Type: System.Web.UI.WebControls.FontInfo
A FontInfo that represents the font properties of the Web server control.

Use the Font property to specify the font properties of the Web Server control. This property includes subproperties that can be accessed declaratively in the form of Property-Subproperty (for example Font-Bold) or programmatically in the form of Property.Subproperty (for example Font.Bold).

All but one subproperty will render in browsers prior to Microsoft Internet Explorer version 4 for all controls. They are: Bold, Italic, Name, Names, Strikeout, Underline, and Size (but only named font sizes, such as Small, Smaller, and so on, will work).

NoteNote

Although these subproperties render in browsers prior to Microsoft Internet Explorer version 4, the HTML that is rendered is different than later browsers. Instead of rendering as style attributes, these subproperties are rendered as HTML elements, such as <b> and <font>.

The one subproperty that will not render on earlier browsers for all controls is Overline.

The following example illustrates how to use the Font property to gather font information from a Label control.

NoteNote

The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Page Code Model.


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

        void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "The current font is: " + Label1.Font.ToString();
        }

        void Button2_Click(object sender, EventArgs e)
        {
            Label1.Font.Underline = !Label1.Font.Underline;
            if (Label1.Font.Name == "Verdana")
                Label1.Font.Name = "Times";
            else
                Label1.Font.Name = "Verdana";
        }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>Enabled Property Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>FontUnit Property of a Web Control</h3>
        <asp:Label id="Label1" runat="server"
            Font-Names="Verdana" Font-Size="10pt"
            Text="This is a Label control." />  

            <p>
            <asp:Button id="Button1" runat="server"
                Text="Click to display font info"
                OnClick="Button1_Click" Width="300px" />
            </p>

            <p>
            <asp:Button id="Button2" runat="server"
                Text="Click to change font and underlining"
                OnClick="Button2_Click" Width="300px" />
            </p>
    </div>
    </form>
</body>
</html>


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft