Results 1 to 8 of 8

Thread: Adding style sheets to web pages at runtime.

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Adding style sheets to web pages at runtime.

    I have 2 methods here.

    1) Using a literal control

    ASPX Code
    VB Code:
    1. <HEAD>
    2.    <asp:Literal id="StyleSheet" runat="Server" />
    3. </HEAD>

    Code Behind
    VB Code:
    1. Protected WithEvents StyleSheet As System.Web.UI.WebControls.Literal
    2.  
    3. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.     StyleSheet.Text = "<LINK href=Badger.css type=""text/css"" rel=""stylesheet"">"
    5. End Sub

    2) Using code inline

    ASPX Code
    VB Code:
    1. <HEAD>
    2.    <LINK href="<%=gStylesheet%>" type="text/css" rel="stylesheet">
    3. </HEAD>

    Code Behind
    VB Code:
    1. Public gStyleSheet As String
    2.  
    3. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.     gStyleSheet = "Badger.css"
    5. End Sub
    Which one of these methods would people use?

    Also, I don't suppose anyone knows how to write a server side control that would allow me to do the above:
    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     MyStyleSheetControl.CSSFile = "Badger.css"
    3. End Sub

    Regards,

    Woof

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: Adding style sheets to web pages at runtime.

    Personally I would use the HTML approach because you can then change stylesheets and designs without requiring a recompile
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Adding style sheets to web pages at runtime.

    You could even use the HtmlGenericControl like so:
    ASPX Code
    Code:
    <head>
    <link id="css1" runat="server" type="text/css" rel="stylesheet">
    </head>
    Code behind
    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        css1.Attributes("href") = "Badger.css"
    End Sub
    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  5. #5

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Adding style sheets to web pages at runtime.

    dj4uk, thanks for that.
    I think you gave me that method a few months back if I can remember.
    Oh you forgot the following from your above post:
    VB Code:
    1. Protected css1 As HtmlGenericControl
    The is one slight problem with the code you posted, which is why I am re-visiting this problem.
    The line:
    VB Code:
    1. <link id="css1" runat="server" type="text/css" rel="stylesheet">
    Actually fails with a parser error
    It requires a / at the end, like:
    VB Code:
    1. <link id="css1" runat="server" type="text/css" rel="stylesheet"/>
    This now works a treat. Cool

    BUT...thanks to the buggy IDE, .NET randomly sees the LINK tag and says "Oooo...there's a / at the end...I don't need that", and then strips it out.
    This means everytime you compile your app you must sift through the ASPX pages and check to see if the IDE hasn't gone into spak mode

    Don't suppose you know a way around this?

    Woka

  6. #6
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Adding style sheets to web pages at runtime.

    Well spotted - didn't check the code before I posted so I'm not surprised I missed something!

    I don't use the link tag myself - I use the following syntax for external CSS:
    Code:
    <head>
    <style type="text/css">
    	@import url("nutter.css");
    </style>
    </head>
    You could use the HtmlGenericControl here again like so:
    Code:
    <style id="css1" runat="server" type="text/css"></style>
    and then
    Code:
    Protected css1 As HtmlGenericControl
    
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        css1.InnerText = "@import url(""nutter.css"");"
    End Sub
    This might get around the bug you are experiencing with the link tag.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: Adding style sheets to web pages at runtime.

    Quote Originally Posted by Wokawidget
    dj4uk, thanks for that.

    BUT...thanks to the buggy IDE, .NET randomly sees the LINK tag and says "Oooo...there's a / at the end...I don't need that", and then strips it out.
    This means everytime you compile your app you must sift through the ASPX pages and check to see if the IDE hasn't gone into spak mode

    Don't suppose you know a way around this?

    Woka
    The IDE does that because its using the schema as specified in your schema tag (probably a few lines right above that).
    Code:
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    According to that schema, a link tag shouldn't have an inline end slash.

    The file is located (for Vs 2003) at:
    C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Packages\schemas\html\ie5_0.xsd

    If you open it up, and do a find for:
    <xsd:element name="link"

    you will find the attributes that define how VS handles it according to the schema. I don't know much more about it defines it will strip away the inline end tag... but a few hours will probably illuminate the correct one for you. Div's for instance always need a closing tag, so perhaps try comparing it against that.

  8. #8
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: Adding style sheets to web pages at runtime.

    I have used a HttpHandler to dynamicly select/generate the .css file on demand.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width