共用方式為


IStyleSheet.RegisterStyle(Style, IUrlResolutionService) 方法

定義

類別實作時,將新樣式規則加入至網頁 <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

IUrlResolutionService 實作的物件,包含目前位置 (URL) 的內容資訊。

範例

下列程式碼範例會 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 屬性名稱新增至 物件區段 Page 內的 <head> 內嵌樣式表單,並將規則與自動產生的樣式名稱產生關聯。 物件 Style 會使用指定的 urlResolver 參數來轉譯。

如果 urlResolver 設定為 null ,則會使用目前 Page 的 URL。

Style如果物件已註冊,則不會多次加入。

注意

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

適用於