語言

IStyleSheet 介面

定義

定義類別必須實作以支援樣式規則建立的方法。

public interface class IStyleSheet
public interface IStyleSheet
type IStyleSheet = interface
Public Interface IStyleSheet

範例

以下程式碼範例利用 Header 的 IStyleSheet 實作示範程式化地建立新的樣式規則並註冊自訂 Style 物件。

在範例的第一部分,建立一個自訂 Style 物件 labelStyle,然後註冊為當前位置(URL)。 然後標籤 label1 呼叫該 MergeStyle 方法,讓 labelStyle 樣式套用到標籤上 label1 。

範例的第二部分定義了另一個自訂 Style 物件, bodyStyle並設定其屬性以建立新的樣式規則。

Note

此類別主要針對想要自訂實作的開發者設計。 此範例展示了 .NET Framework 所提供的實作。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="istylesheetcs.aspx.cs" Inherits="istylesheetcs" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
    <title>IStyleSheet Example</title>
</head>    
<body>
    <form id="form1" runat="server">
        <h1>IStyleSheet Example</h1>
        <asp:Label 
          id="label1" 
          runat="server">
        </asp:Label>
        <br /><br />
        <asp:Label 
          id="label2" 
          runat="server">
        </asp:Label>
    </form>
  </body>
</html>
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="istylesheetvb.aspx.vb" Inherits="istylesheetvb" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
    <title>IStyleSheet Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <h1>IStyleSheet Example</h1>
        <asp:Label 
          id="label1" 
          runat="server">
        </asp:Label>
        <br /><br />
        <asp:Label 
          id="label2" 
          runat="server">
        </asp:Label>
    </form>
  </body>
</html>

以下是前述網頁的代碼後盾檔案。

public partial class istylesheetcs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Create a Style object to hold style rules to apply to a Label control.
        Style labelStyle = new Style();

        labelStyle.ForeColor = System.Drawing.Color.DarkRed;
        labelStyle.BorderColor = System.Drawing.Color.DarkBlue;
        labelStyle.BorderWidth = 2;

        // Register the Style object so that it can be merged with 
        // the Style object of the controls that use it.
        Page.Header.StyleSheet.RegisterStyle(labelStyle, null);

        // Merge the labelCssStyle style with the label1 control's
        // style settings.
        label1.MergeStyle(labelStyle);
        label1.Text = "This is what the labelCssStyle looks like.";

        // Create a Style object for the <BODY> section of the Web page.
        Style bodyStyle = new Style();

        bodyStyle.ForeColor = System.Drawing.Color.Blue;
        bodyStyle.BackColor = System.Drawing.Color.LightGray;

        // Add the style to the header of the current page.
        Page.Header.StyleSheet.CreateStyleRule(bodyStyle, null, "BODY");

        // Add text to the label2 control to see the label without 
        // the labelStyle applied to it.  
        label2.Text = "This is what the bodyStyle looks like.";
    }
}

備註

實作此介面的類別可利用它來支援樣式規則的建立。

若要自訂層疊樣式表的建立與註冊方式,您必須建立一個實作此介面的類別。

該 HtmlHead 類別透過屬性 Header 實作此介面供 ASP.NET 使用。

Note

在非同步回貼期間,程式化新增或修改樣式或樣式規則並不支援。 當你為 ASP.NET 網頁加入 AJAX 功能時,非同步回貼會更新頁面的部分區域,而不會更新整個頁面。 欲了解更多資訊,請參閱 Microsoft Ajax 概覽。

方法

名稱 Description
CreateStyleRule(Style, IUrlResolutionService, String)

當由類別實作時,會為指定的文件語言元素類型或選擇器建立樣式規則。

RegisterStyle(Style, IUrlResolutionService)

當類別實作時,會在網頁的區塊中為嵌入的樣式表 <head> 新增一條樣式規則。

適用於

另請參閱