Solved

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

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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 style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now