ASP.NET Custom Control - What is the best way to include embedded CSS reference only once?

后端 未结 3 1472
粉色の甜心
粉色の甜心 2020-12-31 15:08

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

3条回答
  •  醉酒成梦
    2020-12-31 15:23

    I don't know why, but your solution didn't work for me, the ClientScript.IsClientScriptBlockRegistered call always returned false. But John Bledsoe's suggestion on the link you already provided (here) worked for me:

    public static void IncludeStylesheet(Page page, string href, string styleID)
    {
        //Prevent the stylesheet being included more than once
         styleID = "_" + styleID;
        if (HttpContext.Current.Items[styleID] == null)
        {
            HtmlLink htmlLink = new HtmlLink();
            htmlLink.Href = href;
            htmlLink.Attributes.Add("rel", "stylesheet");
            htmlLink.Attributes.Add("type", "text/css");
            page.Header.Controls.Add(htmlLink);
            HttpContext.Current.Items[styleID] = true;
        }
    }
    

提交回复
热议问题