Take the 2-minute tour ×

I need to add a HTML style container in the section of a particular page, like so:


Although there are quite a few ways of doing this I was thinking about instantiating a HtmlControl from the System.Web.UI.HtmlControls namespace and simply render it on the page. However I only found the HtmlGenericControl to be the closest candidate - is there a more suitable control I can use or do I have to use another approach?

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Try something like

HtmlGenericControl style = new HtmlGenericControl();
style.TagName = "style";
style.Attributes.Add("type", "text/css");
style.InnerHtml = "body{background-color:#000000;}";


Ivo Stoykov

share|improve this answer
even though this is perfectly acceptable approach, it will create additional style block in the head and not reusing one already present, if one exists –  Kris Ivanov Mar 19 '11 at 15:24
Is that really a problem? –  Kyle Trauberman Mar 19 '11 at 15:34
not really, unless you are doing something on client side that expects single container for style in the head, of course there is optimization consideration where if you are adding large amount of style rules each will be wrapped in its own style block, in addition since most of the content is non-compiled code it would be easier to introduce syntactical errors –  Kris Ivanov Mar 19 '11 at 15:37
It looks like the w3 specification on HTML 4.01 allows for multiple style containers in the head section so I don't think it really matters. Personally I would probably be more inclined to add my own style container in the head section, especially if I knew I didn't have complete control over the rendering of that page. Again, this is a simple example and there's a lot more to it. –  boomshanka Mar 19 '11 at 16:05
add comment

you can try this:

    var myStyle =
        new Style { ForeColor = System.Drawing.Color.FromArgb(255, 0, 0) };

    Page.Header.StyleSheet.CreateStyleRule(myStyle, this, ".myStyle");
share|improve this answer
sorry K Ivanov, I posted this without thinking straight. Will this approach still work if the content is not supported by the Style class (referencing the control by id instead of class attribute)? –  boomshanka Mar 19 '11 at 15:31
same thing apply to any selector, which is the 3rd parameter of IStyleSheet.CreateStyleRule Method, just pass "#mycontrol" –  Kris Ivanov Mar 19 '11 at 15:39
okay, but what about CSS attributes that are not covered by a property, like "visibility"? –  boomshanka Mar 19 '11 at 15:48
that is the base class for other more specific control styles, perhaps if you tell us how this is going to be used, we can give you more precise answer –  Kris Ivanov Mar 19 '11 at 17:15
add comment

You can use an HtmlGenericControl - or you can use a Literal if you want to.

A better way might be to insert this into your HTML Header using something like this code from http://msdn.microsoft.com/en-us/library/system.web.ui.page.header.aspx

  protected void Page_Load(object sender, System.EventArgs e)

      // Create a Style object for the body of the page.
      Style bodyStyle = new Style();

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

      // Add the style rule named bodyStyle to the header 
      // of the current page. The rule is for the body HTML element.
      Page.Header.StyleSheet.CreateStyleRule(bodyStyle, null, "body");

      // Add the page title to the header element.
      Page.Header.Title = "HtmlHead Example"; 

share|improve this answer
As I wrote to K Ivanov I posted this without thinking straight. I updated the example so that it's more precise :-) –  boomshanka Mar 19 '11 at 15:33
CreateStyleRule should still work OK for you - just use "#mycontrol" for the third parameter. –  Stuart Mar 19 '11 at 15:42
I noticed that there are only a handful of properties mapped to the CSS attribute. For instance I couldn't find any property mapped to "display". Do I have to add this another way? –  boomshanka Mar 19 '11 at 16:08
This post - stackoverflow.com/questions/657144/… - suggests you can use .Add() on the Style object? Not sure that works though - sorry :/ –  Stuart Mar 19 '11 at 17:22
add comment

Your Answer


