共用方式為


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) 的內容資訊。

範例

下列程式代碼範例會使用 的HeaderIStyleSheet實作來示範如何建立自定義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 概觀

適用於