How can I register a css code block inside an ascx control?
Can I just have
<head id="head" runat="server">
<style type="text/css">
.customClass
{
background-color: Lime;
}
</style>
</head>
Anywhere in ascx page? I doesn't seem to work?
Styles must be defined in the head
section of your HTML. That goes for both style
tags and link
tags that register external CSS files.
If your page has a head
tag with the runat="server"
attribute, you can programmatically access it via the property this.Page.Header
.
The method I usually use when I need to add something between the opening <head>
and closing </head>
tag is a method such as this one. Simply pass in the url to your stylesheet.
public void AddStylesheet(string url)
{
string link = String.Format("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", url);
this.Page.Header.Controls.Add(new LiteralControl { Text = link });
}