IStyleSheet.RegisterStyle(Style, IUrlResolutionService) Método

Definição

Quando implementado por uma classe, adiciona uma nova regra de estilo à folha de estilo inserida na seção <head> de uma página da Web.

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)

Parâmetros

style
Style

A regra de estilo a ser adicionada à folha de estilo inserida.

urlResolver
IUrlResolutionService

Um objeto implementado por IUrlResolutionService que contém as informações de contexto para o local atual (URL).

Exemplos

O exemplo de código a seguir usa a Header implementação para demonstrar a criação de IStyleSheet um objeto personalizado Style e, em seguida, labelStyleregistrá-lo para o local atual (URL). Em seguida, o label1 rótulo chama o MergeStyle método para que o labelStyle estilo seja aplicado ao label1 rótulo.

<%@ 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>

Comentários

Esse método adiciona uma nova regra de estilo e RegisteredCssClass um nome de propriedade à folha de estilos inserida na <head> seção de um Page objeto e associa a regra a um nome de estilo gerado automaticamente. O Style objeto é renderizado usando o parâmetro especificado urlResolver .

Se urlResolver estiver definido como null, a URL da corrente Page será usada.

Se um Style objeto já estiver registrado, ele não será adicionado várias vezes.

Observação

Não há suporte para adicionar ou modificar estilos programaticamente durante postbacks assíncronos. Quando você adiciona recursos do AJAX a uma página da Web ASP.NET, postbacks assíncronos atualizam regiões da página sem atualizar a página inteira. Para obter mais informações, consulte a Visão geral do Microsoft Ajax.

Aplica-se a