5

I use the code bellow to dynamically include a CSS file:

HtmlHead head = (HtmlHead)Page.Header;
HtmlLink link = new HtmlLink();
link.Attributes.Add("href", Page.ResolveClientUrl("~/App_Themes/Default/StyleSheet.css"));
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
head.Controls.Add(link);

The problem is: I want to do it only once, and only if it isn't alrealy included in the page.

How do I verify if it is already included?

Edit:

Answers telling me to include in page load using !IsPostBack won't solve my problem, as this code will be inside a Web User Control and my page may have a lot of the same user control.

For example, I use the code below to do it with javascript:

if (!Page.ClientScript.IsClientScriptIncludeRegistered("jsScript"))
{
    Page.ClientScript.RegisterClientScriptInclude("jsScript", ResolveUrl("~/Utilities/myScript.js"));
}
Matthieu Cormier
  • 2,185
  • 1
  • 21
  • 32
Tufo
  • 494
  • 7
  • 22
  • If your CSS is reasonably-well structured, this is probably a premature optimization. I can't imagine you using so much CSS that it would significantly slow down the browser. – Robert Harvey Mar 02 '10 at 20:48
  • 1
    @Robert Harvey: I know man, probably it won't slow down my app, I just don't like the idea of putting junk-code in my page, today I'm putting a CSS with only 5KB, but tomorrow when I put ont with 50KB, so it'll be a problem... – Tufo Mar 02 '10 at 20:57
  • @Jason: So I need to be more likely to post more simple questions? if you look at my questions you'll realize that they're all complex and no one answered something that solved the problem. – Tufo Mar 02 '10 at 20:58
  • From your example you're using the App_Themes directory. By using Themes, ASP.NET will automatically link all stylesheets in the theme to your page. You just need to set the Theme or StylesheetTheme attribute of the @Page directive to "Default" to make this work. If you don't want that behavior, you should consider moving your stylesheets outside of the App_Themes directory. – Greg Mar 02 '10 at 21:51

3 Answers3

6

Did it...

the code I used is as follows:

        Boolean cssAlrealyIncluded = false;
        HtmlLink linkAtual;
        foreach (Control ctrl in Page.Header.Controls)
        {
            if (ctrl.GetType() == typeof(HtmlLink))
            {
                linkAtual = (HtmlLink)ctrl;

                if (linkAtual.Attributes["href"].Contains("datePicker.css"))
                {
                    cssAlrealyIncluded = true;
                }
            }
        }

        if (!cssAlrealyIncluded)
        {
            HtmlLink link = new HtmlLink();
            link.Attributes.Add("href", ResolveUrl("~/Utilities/datePickerRsx/datePicker.css"));
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            Page.Header.Controls.Add(link);
        }
Tufo
  • 494
  • 7
  • 22
3

Why not in your user control, add a value to HttpContext.Current.Items indicating that the stylesheet has already been included? This will prevent you from needing to look at every header control for every instance of the user control.

John Bledsoe
  • 17,142
  • 5
  • 42
  • 59
  • This worked best for me. See also http://stackoverflow.com/questions/3876601/asp-net-custom-control-what-is-the-best-way-to-include-embedded-css-reference-o/6623069#6623069 – Colin Jul 08 '11 at 10:19
0

In most cases you shouldn't care if CSS gets included more than once. It's generally not a problem.

EDIT: Order only matters if you need to be able to override css styles in subsequent style sheets.

In ASP.NET, you can include the CSS in your masterpage (assuming you have one) and then it will be guaranteed to only be included once. Since masterpages are available programmatically (even from user controls), you could even write some properties (or methods) that allow you to control which CSS externals to include when.

LBushkin
  • 129,300
  • 32
  • 216
  • 265
  • 2
    I would disagree on the basis of *most cases*, in *any case* where you want to override some CSS behavior, say any site with a theme, order matters and this would be an issue. – Nick Craver Mar 02 '10 at 20:53
  • @lBushkin: If I use master pages for my user controls will only make me having multiple copies of the master page, as each user control will generate its own master page compiled code at runtime, and I'll face the same problem as in this question... – Tufo Mar 02 '10 at 21:01
  • @Tufo: You define a master page for your pages, not your user controls. Master pages can inherit from one another, but ultimately, there aren't multiple instances of the master page per UC. – LBushkin Mar 02 '10 at 21:10