Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now