Language

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実装されたオブジェクト。

例

次のコード例では、HeaderのIStyleSheet実装を使用して、カスタム 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 パラメーターを使用してレンダリングされます。

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

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

Note

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

適用対象