Export (0) Print
Expand All

WebControl.Font Property

Gets the font properties associated with the Web server control.

[Visual Basic]
Public Overridable ReadOnly Property Font As FontInfo
[C#]
public virtual FontInfo Font {get;}
[C++]
public: __property virtual FontInfo* get_Font();
[JScript]
public function get Font() : FontInfo;

Property Value

A FontInfo that represents the font properties of the Web server control.

Remarks

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).

Note   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.

Example

[Visual Basic, C#, JScript] The following example illustrates how to use the Font property to gather font information from a Label control.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
 <head>
    <script language="VB" runat="server">
 
        Sub Button1_Click(sender As Object, e As EventArgs)
            Label1.Text = "The font this label uses is: " & Label1.Font.ToString()
        End Sub

        Sub Button2_Click(sender As Object, e As EventArgs)
            Label1.Font.Underline = True
        End Sub
 
    </script>
 
 </head>
 <body>
    <h3>FontUnit Property of a Web Control</h3>
 <form runat="server">
 
    <asp:Label id="Label1" 
     Font-Name="Verdana" Font-Size="10pt"
     Text="This is a Label control."
     runat="server"/>  
 
    <p>
    <asp:Button id="Button1"
     Text="Click to gather font info of the label control"
     OnClick="Button1_Click" 
     Width="300px"
     runat="server"/>
 
    <p>
    <asp:Button id="Button2"
     Text="Click to underline the text of the label control"
     OnClick="Button2_Click" 
     Width="300px"
     runat="server"/>
 
 </form>
 </body>
 </html>
 

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
 <head>
    <script language="C#" runat="server">
 
        void Button1_Click(Object sender, EventArgs e) {
           Label1.Text = "The font this label uses is: " + Label1.Font.ToString();
        }
 
        void Button2_Click(Object sender, EventArgs e) {
           Label1.Font.Underline = true;
        }
 
 
    </script>
 
 </head>
 <body>
    <h3>FontUnit Property of a Web Control</h3>
 <form runat="server">
 
    <asp:Label id="Label1" 
     Font-Name="Verdana" Font-Size="10pt"
     Text="This is a Label control."
     runat="server"/>  
 
    <p>
    <asp:Button id="Button1"
     Text="Click to gather font info of the label control"
     OnClick="Button1_Click" 
     Width="300px"
     runat="server"/>
 
    <p>
    <asp:Button id="Button2"
     Text="Click to underline the text of the label control"
     OnClick="Button2_Click" 
     Width="300px"
     runat="server"/>
 
 </form>
 </body>
 </html>
 

[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>

<html>
 <head>
    <script language="JSCRIPT" runat="server">
 
        function Button1_Click(sender : Object, e : EventArgs){
            Label1.Text = "The font this label uses is: " + Label1.Font.ToString()
        }

        function Button2_Click(sender : Object, e : EventArgs){
            Label1.Font.Underline = true
        }
 
    </script>
 
 </head>
 <body>
    <h3>Font Property of a Web Control</h3>
 <form runat="server">
 
    <asp:Label id="Label1" 
     Font-Name="Verdana" Font-Size="10pt"
     Text="This is a Label control."
     runat="server"/>  
 
    <p>
    <asp:Button id="Button1"
     Text="Click to gather font info of the label control"
     OnClick="Button1_Click" 
     Width="300px"
     runat="server"/>
 
    <p>
    <asp:Button id="Button2"
     Text="Click to underline the text of the label control"
     OnClick="Button2_Click" 
     Width="300px"
     runat="server"/>
 
 </form>
 </body>
 </html>
 

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

WebControl Class | WebControl Members | System.Web.UI.WebControls Namespace | Style | FontInfo

Show:
© 2014 Microsoft