Edit

Share via


IStyleSheet.CreateStyleRule(Style, IUrlResolutionService, String) Method

Definition

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

public:
 void CreateStyleRule(System::Web::UI::WebControls::Style ^ style, System::Web::UI::IUrlResolutionService ^ urlResolver, System::String ^ selector);
public void CreateStyleRule (System.Web.UI.WebControls.Style style, System.Web.UI.IUrlResolutionService urlResolver, string selector);
abstract member CreateStyleRule : System.Web.UI.WebControls.Style * System.Web.UI.IUrlResolutionService * string -> unit
Public Sub CreateStyleRule (style As Style, urlResolver As IUrlResolutionService, selector As String)

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

selector
String

The part of the HTML page affected by the style.

Examples

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" "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 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.";
    }
  }

</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 for the <body> section of the Web page.
      Dim bodyStyle As Style = 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, me, "BODY")

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

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.

Note

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.

Applies to