IStyleSheet 接口

定义

定义某个类为支持样式规则的创建而必须实现的方法。Defines the methods that a class must implement in order to support the creation of style rules.

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

示例

下面的代码示例使用HeaderIStyleSheet实现来演示如何以编程方式创建新样式规则并注册自Style定义对象。The following code example uses the Header implementation of IStyleSheet to demonstrate programmatically creating a new style rule and registering the custom Style object.

在此示例的第一部分, 将创建一个Style自定义labelStyle对象, 然后为当前位置 (URL) 注册该对象。In the first part of the example, a custom Style object, labelStyle, is created and then registered for the current location (URL). 然后, label1 MergeStyle标签调用方法, 以便labelStyle将样式应用label1于标签。Then the label1 label calls the MergeStyle method so that the labelStyle style is applied to the label1 label.

该示例的第二部分定义另一个Style自定义bodyStyle对象, 并设置其属性来创建新的样式规则。The second part of the example defines another custom Style object, bodyStyle,and sets its properties to create a new style rule.

备注

此类主要适用于想要创建自定义实现的开发人员。This class is intended primarily for developers that want to create a custom implementation. 此示例演示 .NET Framework 提供的实现。This example demonstrates the implementation provided by the .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>

下面是前面示例中网页的代码隐藏文件。The following is the code-behind file for the Web page in the preceding example.

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.";
    }
}

注解

实现此接口的类可使用它来支持创建样式规则。Classes that implement this interface can use it to support the creation of style rules.

若要自定义创建和注册级联样式表的方式, 必须创建一个实现此接口的类。To customize the way cascading style sheets are created and registered, you must create a class that implements this interface.

类实现此接口, 供 ASP.NET Header通过属性使用。 HtmlHeadThe HtmlHead class implements this interface for use by ASP.NET through the Header property.

备注

不支持在异步回发过程中以编程方式添加或修改样式或样式规则。Adding or modifying styles or style rules programmatically during asynchronous postbacks is not supported. 将 AJAX 功能添加到 ASP.NET 网页时, 异步回发会更新页面区域, 而无需更新整个页面。When you add AJAX capabilities to an ASP.NET Web page, asynchronous postbacks update regions of the page without updating the whole page. 有关详细信息, 请参阅Microsoft Ajax 概述For more information, see Microsoft Ajax Overview.

方法

CreateStyleRule(Style, IUrlResolutionService, String)

由类实现时,它为指定的文档语言元素类型或选择器创建样式规则。When implemented by a class, creates a style rule for the specified document language element type, or selector.

RegisterStyle(Style, IUrlResolutionService)

由类实现时,它将新的样式规则添加至网页的 <head> 部分中的嵌入样式表。When implemented by a class, adds a new style rule to the embedded style sheet in the <head> section of a Web page.

适用于

另请参阅