WebControl.Font WebControl.Font WebControl.Font WebControl.Font Property

定義

取得與 Web 伺服器控制項關聯的字型屬性。Gets the font properties associated with the Web server control.

public:
 virtual property System::Web::UI::WebControls::FontInfo ^ Font { System::Web::UI::WebControls::FontInfo ^ get(); };
public virtual System.Web.UI.WebControls.FontInfo Font { get; }
member this.Font : System.Web.UI.WebControls.FontInfo
Public Overridable ReadOnly Property Font As FontInfo

屬性值

FontInfo,表示 Web 伺服器控制項的字型屬性。A FontInfo that represents the font properties of the Web server control.

範例

下列範例說明如何使用Font屬性, Label從控制項收集字型資訊。The following example illustrates how to use the Font property to gather font information from a Label control.

注意

下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案中,則可能無法正確運作。The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. 此程式碼範例必須複製到具有 .aspx 副檔名的空白文字檔。This code sample must be copied into an empty text file that has an .aspx extension. 如需 Web Forms 程式碼模型的詳細資訊,請參閱ASP.NET Web Form 頁面程式碼模型For more information on the Web Forms code model, see ASP.NET Web Forms 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>
<%@ Page Language="VB" %>

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

<script runat="server">
    Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Label1.Text = "The current font is: " & Label1.Font.ToString()
    End Sub

    Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
        Label1.Font.Underline = Not Label1.Font.Underline
        If Label1.Font.Name = "Verdana" Then
            Label1.Font.Name = "Times"
        Else
            Label1.Font.Name = "Verdana"
        End If
            
    End Sub
</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>

備註

Font使用屬性,即可指定 Web 服務器控制項的字型屬性。Use the Font property to specify the font properties of the Web Server control. 這個Property-Subproperty屬性包括可以用宣告方式( Font-Bold例如)或Property.Subproperty以(例如Font.Bold)的形式以程式設計方式存取的子屬性。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).

在 Microsoft Internet Explorer 4 版之前,所有控制項的瀏覽器都只會轉譯一個子屬性,All but one subproperty will render in browsers prior to Microsoft Internet Explorer version 4 for all controls. 其為: BoldItalicNameSmall Smaller、 、Strikeout和(但只有命名的字型大小,例如、等等)可以使用。Size Names UnderlineThey are: Bold, Italic, Name, Names, Strikeout, Underline, and Size (but only named font sizes, such as Small, Smaller, and so on, will work).

注意

雖然這些子屬性會在 Microsoft Internet Explorer 第4版之前轉譯成瀏覽器,但是轉譯的 HTML 與之後的瀏覽器不同。Although these subproperties render in browsers prior to Microsoft Internet Explorer version 4, the HTML that is rendered is different than later browsers. 這些子屬性會轉譯為 HTML 元素(例如<b><font>),而不是轉譯成樣式屬性。Instead of rendering as style attributes, these subproperties are rendered as HTML elements, such as <b> and <font>.

在舊版瀏覽器上不會針對所有控制項轉譯的一個子Overline屬性是。The one subproperty that will not render on earlier browsers for all controls is Overline.

適用於

另請參閱