Dynamically adding a stylesheet

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

I want to dynamically add a stylesheet using <style type="text/css"></style>
tags. The code that will use this stylesheet will be shared, so I will not
have control over what the user does as far as stuff like adding
runat="server" and id="someid" attributes to the <head> tag of their .aspx
page. I was wondering if there is a way similar to the
RegisterClientScriptBlock method that can be used. Any ideas or suggestions?
Thanks.
 
G

Guest

Actually, I believe you can get away with RegisterClientScriptBlock even
though it is not "script". Try it.
Peter
 
N

Nathan Sokalski

Thanks! It may not be the intended purpose, or even inside the <head> tag,
but it's good enough for me, and it works.
 
F

Flinky Wisty Pomm

I cheat. I've got two repeaters defined in my masterpages, one for
scripts, one for CSS. My base page class has a couple of List<string>'s
and I just call
AddResource("path/to/file", ResourceType.Css) to add resources to those
Lists

Then I bind the repeaters on PreRender so any page events have an
opportunity to add CSS/Js that'll be needed on the client. It's a hack,
but I like my stylesheets and scripts in external files referenced in
the head where they belong.
 
E

Edwin Knoppert

I don't get it but <%= ... > should do??
I'm using it for resolving the script url.
 
N

Nathan Sokalski

<%=...%> would be fine if I was writing the webform that used the
stylesheet, but I am not writing the webform, I am writing a couple external
classes that will be used with webforms.
 
M

Martijn Saly

Flinky said:
I cheat. I've got two repeaters defined in my masterpages, one for
scripts, one for CSS. My base page class has a couple of List<string>'s
and I just call
AddResource("path/to/file", ResourceType.Css) to add resources to those
Lists

Then I bind the repeaters on PreRender so any page events have an
opportunity to add CSS/Js that'll be needed on the client. It's a hack,
but I like my stylesheets and scripts in external files referenced in
the head where they belong.

That sounds like a much better solution. A <script> tag inside the body is
ugly (and invalid XHTML, I believe). And a <style> tag in the body is just
invalid in both HTML and XHTML.

And we all know that production-quality controls generate valid XHTML at
least, right? ;)
 

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
44,998
Latest member
MarissaEub

Latest Threads

Top