Solved

How to determine if a CSS file has been previously registered?

Posted on 2008-10-16
3
562 Views
Last Modified: 2013-12-17
I am writing a Server Custom Control and I am embedding some resources in it.  As part of the process, a CSS resource is copied to the temporary folder.  I then use the following code to register it.

string includeTemplate ="<link rel='stylesheet' text='text/css' href='{0}' />";
string includeLocation = Page.ClientScript.GetWebResourceUrl(this.GetType(), "myStylesheet _Links.css");
LiteralControl include = new LiteralControl(String.Format(includeTemplate, includeLocation));
((System.Web.UI.HtmlControls.HtmlHead)Page.Header).Controls.Add(include);

What I need to do is to determine if the file is already registered with the page.  How can I do this?
0
Comment
Question by:DeHaynes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 22735963
(if (Page[this.ClientID] == null)
{
    //not added

Page[this.ClientID] = true;
//your code  
}
0
 
LVL 3

Expert Comment

by:JunkMan
ID: 22735969
You're better off using a HtmlLink control instead of a Literal, that way you'll be able to grab it back an check if the reference has been added before or not.
        bool addIt = true;
        string styleSheet = Page.ClientScript.GetWebResourceUrl(this.GetType(), "myStylesheet _Links.css");
        foreach (Control c in Page.Header.Controls)
        {
            HtmlLink l = c as HtmlLink;
            if (null != l && styleSheet.Equals(l.Href, StringComparison.OrdinalIgnoreCase))
            {
                addIt = false;
                break;
            }
        }
        if (addIt)
        {
            HtmlLink link = new HtmlLink();
            link.Href = styleSheet;
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            Page.Header.Controls.Add(link);
        }

Open in new window

0
 
LVL 8

Accepted Solution

by:
rlh68 earned 250 total points
ID: 22737231

    string cssHref = "myStylesheet _Links.css";
    if (!this.Page.IsClientScriptBlockRegistered(cssHref)) {
        string cssScript = "<link rel='stylesheet' text='text/css' href='" + cssHref + "' />";
        this.Page.RegisterClientScriptBlock(cssHref, cssScript);
    }
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question