IStyleSheet.CreateStyleRule Method (Style, IUrlResolutionService, String)


When implemented by a class, creates a style rule for the specified document language element type, or selector.

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

void CreateStyleRule(
	Style style,
	IUrlResolutionService urlResolver,
	string selector


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).

Type: System.String

The part of the HTML page affected by the style.

The CreateStyleRule method creates a style rule for the specified selector. Multiple style rules can be created for the same selector.

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


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 defining a new Style object, bodyStyle. Then bodyStyle is added to the current URL by calling CreateStyleRule.

<%@ 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 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, this, "BODY");

      // Add text to the label2 control to see the style rules applied to it.
      label1.Text = "This is what the bodyStyle 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