Share via


IStyleSheet.RegisterStyle(Style, IUrlResolutionService) メソッド

定義

クラスによって実装された場合、Web ページの <head> セクション内の埋め込みスタイル シートに新しいスタイル ルールを追加します。

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

パラメーター

style
Style

埋め込みスタイル シートに追加されるスタイル ルール。

urlResolver
IUrlResolutionService

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

次のIStyleSheetコード例では、実装をHeader使用してカスタム Style オブジェクトを作成し、labelStyle現在の場所 (URL) に登録します。 その後、ラベルは label1 メソッドを MergeStyle 呼び出して、 labelStyle スタイルがラベルに label1 適用されるようにします。

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

</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 to hold style rules to apply to a Label control.
      Dim labelStyle As Style = 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, Nothing)

      ' Merge the labelCssStyle style with the label1 control's
      ' style settings.
      label1.MergeStyle(labelStyle)
      label1.Text = "This is what the labelCssStyle 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>

注釈

このメソッドは、オブジェクトのセクション内の埋め込みスタイル シートに新しいスタイルルールとRegisteredCssClassプロパティ名を<head>Page追加し、自動生成されたスタイル名にルールを関連付けます。 オブジェクトは Style 、指定された urlResolver パラメーターを使用してレンダリングされます。

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

オブジェクトが既に Style 登録されている場合、複数回追加されません。

注意

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

適用対象