Edit

Share via


IStyleSheet.RegisterStyle(Style, IUrlResolutionService) Method

Definition

When implemented by a class, adds a new style rule to the embedded style sheet in the <head> section of a Web page.

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)

Parameters

style
Style

The style rule to be added to the embedded style sheet.

urlResolver
IUrlResolutionService

An IUrlResolutionService-implemented object that contains the context information for the current location (URL).

Examples

The following code example uses the Header implementation of IStyleSheet to demonstrate creating a custom Style object, labelStyle, and then registering it for the current location (URL). Then the label1 label calls the MergeStyle method so that the labelStyle style is applied to the label1 label.

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

Remarks

This method adds a new style rule and RegisteredCssClass property name to the embedded style sheet within the <head> section of a Page object, and associates the rule with an auto-generated style name. The Style object is rendered using the specified urlResolver parameter.

If urlResolver is set to null, the URL of the current Page is used.

If a Style object is already registered, it is not added multiple times.

Note

Adding or modifying styles programmatically during asynchronous postbacks is not supported. When you add AJAX capabilities to an ASP.NET Web page, asynchronous postbacks update regions of the page without updating the whole page. For more information, see Microsoft Ajax Overview.

Applies to