IStyleSheet Interface
Collapse the table of content
Expand the table of content

IStyleSheet Interface

Defines the methods that a class must implement in order to support the creation of style rules.

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

public interface IStyleSheet

The IStyleSheet type exposes the following members.

Public methodCreateStyleRuleWhen implemented by a class, creates a style rule for the specified document language element type, or selector.
Public methodRegisterStyleWhen implemented by a class, adds a new style rule to the embedded style sheet in the <head> section of a Web page.

Classes that implement this interface can use it to support the creation of style rules.

To customize the way cascading style sheets are created and registered, you must create a class that implements this interface.

The HtmlHead class implements this interface for use by ASP.NET through the Header property.


Adding or modifying styles or style rules 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 programmatically creating a new style rule and registering the custom Style object.

In the first part of the example, a custom Style object, labelStyle, is created and then registered for the current location (URL). Then the label1 label calls the MergeStyle method so that the labelStyle style is applied to the label1 label.

The second part of the example defines another custom Style object, bodyStyle,and sets its properties to create a new style rule.


This class is intended primarily for developers that want to create a custom implementation. This example demonstrates the implementation provided by the .NET Framework.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="istylesheetcs.aspx.cs" Inherits="istylesheetcs" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

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

The following is the code-behind file for the Web page in the preceding example.

public partial class istylesheetcs : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        // 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.";

        // Create a Style object for the <BODY> section of the Web page.
        Style bodyStyle = new Style();

        bodyStyle.ForeColor = System.Drawing.Color.Blue;
        bodyStyle.BackColor = System.Drawing.Color.LightGray;

        // Add the style to the header of the current page.
        Page.Header.StyleSheet.CreateStyleRule(bodyStyle, null, "BODY");

        // Add text to the label2 control to see the label without 
        // the labelStyle applied to it.  
        label2.Text = "This is what the bodyStyle looks like.";

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2016 Microsoft