Loading a CSS stylesheet from a Control
Another related issue I ran into today as I was building a small wrapper control around the
jQuery Calendar control is how do you effectively load a CSS style sheet from a custom control, without actually rendering the style sheet multiple times?
The problem here is that unlike Script references which have a clear API in the Page.ClientScript/ScriptManager which help to make sure you don't load multiple references to the same script files, there's no corresponding API for CSS files. However, you can fake out ASP.NET by using the ClientScript/ScriptManager for this anyway:
string css =
@"<link href=""" +
this.ResolveUrl(
"scripts/jquery-calendar.css") +
@""" type=""text/css"" rel=""stylesheet"" />";
ScriptManager.RegisterClientScriptBlock(
this.Page,
typeof(
ControlResources),
"_calcss", css,
false);
You can also use Page.ClientScript.RegisterClientScriptBlock. Basically you can use this API to inject anything into the top of the page, just after the FORM tag as long as you pass the last parameter as false, which indicates that you're providing your own <script> tags - or that you are basically handling the full markup. The flag is there for compatibility with .NET 1.1 which requires that you add the script tags, but you can use this now for injecting code into the page. This isn't ideal for CSS in all cases though: if your CSS needs to modify the body tag then this might not work correctly and as mentioned this is not XHTML compliant.
Partager