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?

0

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

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

you can try this:

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

    Page.Header.StyleSheet.CreateStyleRule(myStyle, this, ".myStyle");

4 Comments

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)?
same thing apply to any selector, which is the 3rd parameter of IStyleSheet.CreateStyleRule Method, just pass "#mycontrol"
okay, but what about CSS attributes that are not covered by a property, like "visibility"?
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
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"; 

  }

4 Comments

As I wrote to K Ivanov I posted this without thinking straight. I updated the example so that it's more precise :-)
CreateStyleRule should still work OK for you - just use "#mycontrol" for the third parameter.
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?
This post - stackoverflow.com/questions/657144/… - suggests you can use .Add() on the Style object? Not sure that works though - sorry :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.