共用方式為


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 頁面中受樣式影響的部分。

範例

下列程式代碼範例會使用 HeaderIStyleSheet 實作來示範如何定義新的 Style 物件 bodyStyle 然後 bodyStyle 藉由呼叫 CreateStyleRule來新增至目前的URL。

<%@ 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。

注意

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

適用於