|
-
May 18th, 2005, 04:12 AM
#1
Adding style sheets to web pages at runtime.
I have 2 methods here.
1) Using a literal control
ASPX Code
VB Code:
<HEAD>
<asp:Literal id="StyleSheet" runat="Server" />
</HEAD>
Code Behind
VB Code:
Protected WithEvents StyleSheet As System.Web.UI.WebControls.Literal
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
StyleSheet.Text = "<LINK href=Badger.css type=""text/css"" rel=""stylesheet"">"
End Sub
2) Using code inline
ASPX Code
VB Code:
<HEAD>
<LINK href="<%=gStylesheet%>" type="text/css" rel="stylesheet">
</HEAD>
Code Behind
VB Code:
Public gStyleSheet As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gStyleSheet = "Badger.css"
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:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyStyleSheetControl.CSSFile = "Badger.css"
End Sub
Regards,
Woof
-
May 18th, 2005, 04:34 AM
#2
Retired VBF Adm1nistrator
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]
-
May 18th, 2005, 04:45 AM
#3
Re: Adding style sheets to web pages at runtime.
Errr...HTML metho? Method 1 or 2?
Woka
-
May 18th, 2005, 04:55 AM
#4
Frenzied Member
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!
-
May 18th, 2005, 05:28 AM
#5
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:
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:
<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:
<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
-
May 18th, 2005, 05:38 AM
#6
Frenzied Member
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!
-
May 20th, 2005, 01:46 PM
#7
I wonder how many charact
Re: Adding style sheets to web pages at runtime.
 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.
-
May 20th, 2005, 10:15 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|