Dynamically Apply CSS

R

Robert

Are there any other ways to dynamically apply CSS styling to a page (without
using inline CSS)?

I'm sure I could dynamically generate a new and uniquely named CSS file "on
the fly" when users log in, and then dynamically associate that css file
with the requested pages - but I'm looking for something much simpler and
with better runtime performance.

Here's what I'm after. I plan to let users save various preferences between
sessions. Behind the scenes I save this information with their account data
in a database. Some of these preferences include page background color and
font size - which I want to apply to their pages at runtime via CSS. There
will also be a couple of css classes to be applied to various elements in
the page.

I'm already storing all of the css settings and classes in the database, and
I'd like to leverage it all from there if possible.

Any suggestions for accomplishing this?

Thanks!
 
A

AsyaHubby

just add attributes to objects at runtime.

txt.attributes.add("Style", YourStyleStringHere)

This will render the style attribute for the input tag with the given
settings
 
R

Robert

Thanks for the quick and useful response - I'll certainly use it in some
scenarios: A couple of followup questions:
1. Will this method work for the <body> element? (I definitely need to be
able to set the <body bgcolor=...>) or will this only work for server
controls?
2. Will this method let me associate a css class with the element being
styled - or am I limited only to applying inline css attributes?

Thanks!
 
H

Hans Kesting

Robert said:
Are there any other ways to dynamically apply CSS styling to a page (without
using inline CSS)?

I'm sure I could dynamically generate a new and uniquely named CSS file "on
the fly" when users log in, and then dynamically associate that css file
with the requested pages - but I'm looking for something much simpler and
with better runtime performance.

Here's what I'm after. I plan to let users save various preferences between
sessions. Behind the scenes I save this information with their account data
in a database. Some of these preferences include page background color and
font size - which I want to apply to their pages at runtime via CSS. There
will also be a couple of css classes to be applied to various elements in
the page.

I'm already storing all of the css settings and classes in the database, and
I'd like to leverage it all from there if possible.

Any suggestions for accomplishing this?

Thanks!

Instead of writing user-specific files to the filesystem, you could also
point the stylesheet to an fixed aspx file:
<link rel="stylesheet" type="text/css" href="usercss.aspx">

and serve the user-specific css code (with appropriate caching) from there.

Hans Kesting
 
A

AsyaHubby

Depends if you are using master template where you write out your own body
tag.
If you are you can easily massage the attribute of the body tag.
You can associate a class - just Object.CssClass=YourClass or you can use as
before
Object.Attributes.Add("Class",YourClass)
 

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,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top