Solved

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

Posted on 2008-10-16
3
560 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

825 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