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并设置其属性以创建新样式规则。

备注

此类主要用于想要创建自定义实现的开发人员。 此示例演示.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类实现此接口,供 ASP.NET 通过Header属性使用。

备注

不支持在异步回发期间以编程方式添加或修改样式或样式规则。 将 AJAX 功能添加到 ASP.NET 网页时,异步回发会更新页面的区域,而无需更新整个页面。 有关详细信息,请参阅 Microsoft Ajax 概述

方法

CreateStyleRule(Style, IUrlResolutionService, String)

由类实现时,它为指定的文档语言元素类型或选择器创建样式规则。

RegisterStyle(Style, IUrlResolutionService)

由类实现时,它将新的样式规则添加至网页的 <head> 部分中的嵌入样式表。

适用于

另请参阅