Search code examples
c#htmlcontrols

How do I add a html style container using C#?


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?


Solution

  • 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