語言

IStyleSheet.CreateStyleRule(Style, IUrlResolutionService, String) 方法

定義

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

public:
 void CreateStyleRule(System::Web::UI::WebControls::Style ^ style, System::Web::UI::IUrlResolutionService ^ urlResolver, System::String ^ selector);
public void CreateStyleRule(System.Web.UI.WebControls.Style style, System.Web.UI.IUrlResolutionService urlResolver, string selector);
abstract member CreateStyleRule : System.Web.UI.WebControls.Style * System.Web.UI.IUrlResolutionService * string -> unit
Public Sub CreateStyleRule (style As Style, urlResolver As IUrlResolutionService, selector As String)

參數

style
Style

樣式規則將加入嵌入的樣式表中。

urlResolver
IUrlResolutionService

一個 IUrlResolutionService包含當前位置上下文資訊的 -實作物件(URL)。

selector
String

HTML 頁面中受樣式影響的部分。

範例

以下程式碼範例使用Header了 的IStyleSheet實作來示範如何定義一個新的Style物件。 bodyStyle 然後 bodyStyle 透過呼叫 CreateStyleRule加入 。

<%@ 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 Page_Load(object sender, EventArgs e)
  {
    if (Page.Header != null)
    {
      // 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, this, "BODY");

      // Add text to the label2 control to see the style rules applied to it.
      label1.Text = "This is what the bodyStyle looks like.";
    }
  }

</script>

<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>
    </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 Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  
    If Not Page.Header Is Nothing Then
    
      ' Create a Style object for the <body> section of the Web page.
      Dim bodyStyle As Style = 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, me, "BODY")

      ' Add text to the label2 control to see the style rules applied to it.
      label1.Text = "This is what the bodyStyle looks like."
      
    End If
    
  End Sub


</script>

<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>
    </form>
  </body>
</html>

備註

此方法為 CreateStyleRule 指定的選擇器建立樣式規則。 同一選擇器可建立多種樣式規則。

若 urlResolver 設為 null,則使用當前 Page 實例的 URL。

Note

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

適用於