IStyleSheet 介面

定義

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

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

範例

下列程式碼範例會使用 HeaderIStyleSheet 實作,以程式設計方式建立新的樣式規則並註冊自訂 Style 物件。

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

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

注意

此類別主要適用于想要建立自訂實作的開發人員。 此範例示範.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 使用。

注意

不支援在非同步回傳期間以程式設計方式新增或修改樣式或樣式規則。 當您將 AJAX 功能新增至 ASP.NET 網頁時,非同步回傳會更新頁面的區域,而不會更新整個頁面。 如需詳細資訊,請參閱Microsoft Ajax 概觀

方法

CreateStyleRule(Style, IUrlResolutionService, String)

類別實作時,建立所指定之文件語言項目型別或選取器的樣式規則。

RegisterStyle(Style, IUrlResolutionService)

類別實作時,將新樣式規則加入至網頁 <head> 區段中的內嵌樣式表。

適用於

另請參閱