共用方式為


WebControl.Font 屬性

定義

取得與網頁伺服器控制項相關的字型屬性。

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

屬性值

A FontInfo 代表網頁伺服器控制項的字型屬性。

範例

以下範例說明如何利用該 Font 屬性從 Label 控制項中收集字型資訊。

備註

以下程式碼範例採用單檔案程式碼模型,若直接複製到程式碼背後的檔案中,可能無法正常運作。 此程式碼範例必須複製到一個具有 .aspx 副檔名的空文字檔中。 欲了解更多關於網頁表單程式碼模型的資訊,請參見 ASP.NET 網頁表單頁面程式碼模型

<%@ 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 Server 控制項的字型屬性。 此特性包含可以宣告形式 Property-Subproperty 存取的子屬性(例如 Font-Bold),或 Property.Subproperty 程式化方式以 (例如 Font.Bold)。 形式存取。

除了一個子屬性外,所有子屬性在 Microsoft Internet Explorer 4 版本之前的瀏覽器中都會渲染。 它們是:BoldItalicUnderlineStrikeoutNamesSizeName(但只有命名字型大小,如 SmallSmaller、 等,才可使用)。

備註

雖然這些子屬性在 Microsoft Internet Explorer 4 版本之前的瀏覽器中會呈現,但所呈現的 HTML 與後來的瀏覽器不同。 這些子屬性不再以樣式屬性渲染,而是以 HTML 元素呈現,例如 <b><font>

唯一無法在早期瀏覽器中呈現所有控制項的子屬性是 Overline

適用於

另請參閱