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

現在の場所 (URL) のコンテキスト情報を格納している IUrlResolutionService 実装オブジェクト。

selector
String

スタイルの影響を受ける HTML ページの部分。

次のコード例では、 の実装をHeader使用して、bodyStyle新しい Style オブジェクト である を定義する方法を示IStyleSheetします。 次 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 、指定したセレクターのスタイル規則を作成します。 同じセレクターに対して複数のスタイル ルールを作成できます。

が にnull設定されている場合urlResolverは、現在Pageのインスタンスの URL が使用されます。

注意

非同期ポストバック中にプログラムでスタイルまたはスタイル ルールを追加または変更することはサポートされていません。 ASP.NET Web ページに AJAX 機能を追加すると、非同期ポストバックはページ全体を更新せずにページの領域を更新します。 詳細については、「Microsoft Ajax の概要」を参照してください。

適用対象