IStyleSheet.RegisterStyle Method (Style, IUrlResolutionService)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)

void RegisterStyle(
	Style style,
	IUrlResolutionService urlResolver


Type: System.Web.UI.WebControls.Style

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

Type: System.Web.UI.IUrlResolutionService

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

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.


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.

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"
<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.Text = "This is what the labelCssStyle looks like.";


<html xmlns="" >
<head id="head1" runat="server">
    <title>IStyleSheet Example</title>
    <form id="form1" runat="server">
        <h1>IStyleSheet Example</h1>

.NET Framework
Available since 2.0
Return to top