The problem: I am embedding a CSS file into a custom control library with several controls. I want to share the same CSS file for all of the controls regardless of how many
To prevent duplication when emitting css files from server controls, we do the following:
if (!Page.ClientScript.IsClientScriptBlockRegistered("SoPro." + id)) {
HtmlLink cssLink = new HtmlLink();
cssLink.Href = cssLink.Href = Page.ClientScript.GetWebResourceUrl(this.GetType(), styleSheet);
cssLink.Attributes.Add("rel", "stylesheet");
cssLink.Attributes.Add("type", "text/css");
this.Page.Header.Controls.Add(cssLink);
this.Page.ClientScript.RegisterClientScriptBlock(typeof(System.Web.UI.Page), "SoPro." + id, String.Empty);
}
First we test to see if the named script was previously registered. If not, we add it in.