Solved

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

Posted on 2008-10-16
3
561 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
How can I make a div layout like the one in the image 2 44
Data organization issue 7 36
CSS Style on Chrome 1 33
How to force output to ascii 2 35
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

756 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