IStyleSheet.RegisterStyle(Style, IUrlResolutionService) Método

Definição

Quando implementado por uma classe, adiciona uma nova regra de estilo à folha de estilo embutida na <head> secção de uma página 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 incorporada.

urlResolver
IUrlResolutionService

Um IUrlResolutionServiceobjeto implementado por -que contém a informação de contexto para a localização atual (URL).

Exemplos

O exemplo de código seguinte utiliza a Header implementação de IStyleSheet para demonstrar a criação de um objeto personalizado Style , labelStyle, e depois o seu registo para a localização atual (URL). Depois, a label1 etiqueta chama o MergeStyle método para que o labelStyle estilo seja aplicado à etiqueta.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>

Observações

Este método adiciona uma nova regra de estilo e RegisteredCssClass nome de propriedade à folha de estilo embutida dentro da <head> secçã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 for definido para null, é usada a URL da corrente Page .

Se um Style objeto já estiver registado, não é adicionado várias vezes.

Note

Não é suportado adicionar ou modificar estilos programaticamente durante postbacks assíncronos. Quando adiciona capacidades AJAX a uma página Web ASP.NET, postbacks assíncronos atualizam regiões da página sem atualizar toda a página. Para mais informações, consulte Microsoft Ajax Overview.

Aplica-se a