Linking to a stylesheet multiple times

N

Nathan Sokalski

I have user controls that contain a link to a stylesheet. Several of my
pages also have a link to this same stylesheet. Because of this, the
resulting output contains multiple links to the same stylesheet. Although
this does not cause any harm as far as how the page looks, it is unnecessary
and makes the download take slightly longer. Is there any easy way to avoid
having the same stylesheet link added multiple times? Thanks.
 
J

JIMCO Software

Nathan said:
I have user controls that contain a link to a stylesheet. Several of
my pages also have a link to this same stylesheet. Because of this,
the resulting output contains multiple links to the same stylesheet.
Although this does not cause any harm as far as how the page looks,
it is unnecessary and makes the download take slightly longer. Is
there any easy way to avoid having the same stylesheet link added
multiple times? Thanks.

Why not just remove the style sheet link from your user control?

--
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com

FrontPage add-ins for FrontPage 2000 - 2003
 
N

Nathan Sokalski

If I removed the stylesheet link from my user control, I would need to
manually add it to any page I decided to add the control to. This somewhat
destroys the purpose of a user control (a control is supposed to be able to
be added without the need to add any other components). Also, doing this
would make it harder for me to edit the control in the future because the
control would be using code (the stylesheet) that is not referenced in the
control's code. Is there any way to do something like programmatically add
the stylesheet to a collection that is a property of the containing Page or
something like that?
 
J

JIMCO Software

Nathan said:
If I removed the stylesheet link from my user control, I would need to
manually add it to any page I decided to add the control to. This
somewhat destroys the purpose of a user control (a control is
supposed to be able to be added without the need to add any other
components). Also, doing this would make it harder for me to edit the
control in the future because the control would be using code (the
stylesheet) that is not referenced in the control's code. Is there
any way to do something like programmatically add the stylesheet to a
collection that is a property of the containing Page or something
like that?

I must have missed the word "several" in your original post.

What about using the runat attribute for your <link> tag so that you can
programmatically configure it?

--
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com

FrontPage add-ins for FrontPage 2000 - 2003
 
N

Nathan Sokalski

How exactly do you mean? I know how to use HtmlGenericControl controls, but
I am not quite sure what you had in mind as far as an algorithm. If you
could explain in more detail what you had in mind, I would appreciate it.
Thanks.
 
G

Guadala Harry

One possibility is to not declare the link. Instead, have a Literal control
where the link would go, and then set it's .Text property in your
code-behind. Your code behind could use whatever logic makes sense in your
application and that logic could insert the link when necessary otherwise
insert nothing. If your page's code-behind is aware of the asxc that it will
be hosting, that "knowledge" could be used to know when to insert the link
and when to not insert it (as just one way to go about this).

//This goes in your aspx <HEAD>:
<asp:Literal id="litCSSFileName" EnableViewState="False"
runat="server"></asp:Literal>

//Then you Page_Load logic could conditionally do something like this:
litCSSFileName.Text = strCssLink;

//Resulting in something like this at runtime in place of the Literal
declaration:
<LINK href="/MyApp/SubFolder/MyCssFile.css" type="text/css"
rel="stylesheet">

-HTH
BTW: It's considered bad etiquette to post same question to multiple news
groups.
 
C

Collin Chung

Hi Nathan

You can use the Page.RegisterStartupScript() from the code behind with
the 'key' and then check to see if scripts (or in this case, styles)
with the same key have already been registered.

eg.

if(!Page.IsStartupScriptRegistered("mycss"))
{
Page.RegisterStartupScript("mycss",@"<style type=""text/css""
src=""styles.css""></style>");
}
 
J

JIMCO Software

Nathan said:
How exactly do you mean? I know how to use HtmlGenericControl
controls, but I am not quite sure what you had in mind as far as an
algorithm. If you could explain in more detail what you had in mind,
I would appreciate it. Thanks.

In your HTML, add something along the lines of this:

<link id="_ctlLink" runat="server" rel="stylesheet" type="text/css" />

Then in your code-behind, do this after your class declaration.

protected System.Web.UI.HtmlControls.HtmlGenericControl _ctlLink;

Now you can add code like this to Page_Load to set the stylesheet:

_ctlLink.Attributes.Add("href", "styles.css");

You can now programmatically control when and what stylesheet gets applied.

--
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com

FrontPage add-ins for FrontPage 2000 - 2003
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top