1

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

<style>
#mycontrol
{
    color:#ff0000;
}
</style>

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?

Stig Perez
  • 3,595
  • 4
  • 23
  • 36

3 Answers3

4

Try something like

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

HTH

Ivo Stoykov

i100
  • 4,529
  • 1
  • 22
  • 20
  • 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
  • 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. – Stig Perez Mar 19 '11 at 16:05
1

you can try this:

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

    Page.Header.StyleSheet.CreateStyleRule(myStyle, this, ".myStyle");
Kris Ivanov
  • 10,476
  • 1
  • 24
  • 35
  • 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)? – Stig Perez 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"? – Stig Perez 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
0

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

  }
Stuart
  • 66,722
  • 7
  • 114
  • 165
  • As I wrote to K Ivanov I posted this without thinking straight. I updated the example so that it's more precise :-) – Stig Perez 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? – Stig Perez Mar 19 '11 at 16:08
  • This post - http://stackoverflow.com/questions/657144/how-do-you-modify-style-in-the-code-behind-file-for-divs-in-asp-net - suggests you can use `.Add()` on the Style object? Not sure that works though - sorry :/ – Stuart Mar 19 '11 at 17:22