Linking to a stylesheet multiple times

Discussion in 'ASP .Net' started by Nathan Sokalski, Sep 14, 2005.

  1. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Sep 14, 2005
    #1
    1. Advertising

  2. Nathan Sokalski wrote:
    > 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
     
    JIMCO Software, Sep 14, 2005
    #2
    1. Advertising

  3. 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?
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "JIMCO Software" <> wrote in message
    news:udf%...
    > Nathan Sokalski wrote:
    >> 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
    >
    >
    >
    >
     
    Nathan Sokalski, Sep 14, 2005
    #3
  4. Nathan Sokalski wrote:
    > 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
     
    JIMCO Software, Sep 14, 2005
    #4
  5. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "JIMCO Software" <> wrote in message
    news:...
    > Nathan Sokalski wrote:
    >> 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
    >
    >
    >
    >
     
    Nathan Sokalski, Sep 14, 2005
    #5
  6. 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.








    "Nathan Sokalski" <> wrote in message
    news:...
    >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.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
     
    Guadala Harry, Sep 14, 2005
    #6
  7. Nathan Sokalski

    Collin Chung Guest

    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>");
    }
     
    Collin Chung, Sep 14, 2005
    #7
  8. Nathan Sokalski wrote:
    > 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
     
    JIMCO Software, Sep 14, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?J=F8rn_Tommy_Kinder=E5s?=

    Stylesheet referanse i stylesheet

    =?ISO-8859-1?Q?J=F8rn_Tommy_Kinder=E5s?=, Jul 3, 2004, in forum: XML
    Replies:
    3
    Views:
    617
    =?ISO-8859-1?Q?J=F8rn_Tommy_Kinder=E5s?=
    Jul 4, 2004
  2. Steven An
    Replies:
    1
    Views:
    542
    Martin Honnen
    Nov 23, 2004
  3. David Blickstein
    Replies:
    3
    Views:
    1,090
    David Blickstein
    Aug 2, 2005
  4. Jack Steven
    Replies:
    2
    Views:
    476
    Chris Rebert
    Mar 9, 2009
  5. Nathan Sokalski

    Linking to a stylesheet multiple times

    Nathan Sokalski, Sep 14, 2005, in forum: ASP .Net Building Controls
    Replies:
    7
    Views:
    156
    JIMCO Software
    Sep 14, 2005
Loading...

Share This Page