Skip navigation.

Adding Variables To Style Sheets

Comment permalink 1 SomeNewKid |
Excellent article, Milan, particularly as it touches on some less-well-known ASP.NET abilities such as XML serialization and custom config sections.

I once came up with a quick-and-dirty method of adding variables to stylesheets. However, your method is indeed much sweeter.

Keep up the great work!
Comment permalink 2 Ryan Farley |
Great stuff (as always) Milan. Rory Blyth did something similar a while back. His approach was not as flexible as yours but still another example for your readers to look at.

http://neopoleon.com/blog/posts/4069.aspx

-Ryan
Comment permalink 3 Bobby |
besides the fact that his downloadable project doesnt work, it would almost sound like a cool trick
Comment permalink 4 Milan Negovan |
Bobby, did you remember to register the HttpModule in web.config?
Comment permalink 5 Bobby |
im using the same web.config file.. this is the error i get:

File or assembly name DynamicCSS, or one of its dependencies, was not found.

Source Error:


Line 8:
Line 9:
Line 10:
Line 11:

Line 12:
Comment permalink 6 Bobby |
well, it didnt show the code above.. but it is the lines in the config fo rthe httpHandlers section
Comment permalink 7 no fun |
Another (easier?) solution could be to just create a file, dynamic.css.aspx, and change the Content-type.

private void Page_Init(object sender, System.EventArgs e)
{
this.ContentType = "text/css";
}


You could even include something like
<%@ OutputCache Duration="86400" VaryByParam="None" Location="ServerAndClient" %>


...and in the file you might have

const string BODY_COLOR = "black";
...
body {
color: [%=BODY_COLOR%]; // couldn't post tag brackets...
}
Comment permalink 8 James |
no fun > I use it every day and even if the first solution is technically interesting, I find the latter easier (few code, no server configuration).

Even I havn't got any "aspx.cs" file, everything is in the "aspx" file.

It helps me to create dynamic stylesheets "on the fly", and does not confuse me with multiple files, because most things are common from one browser to another, except details like font size (mac).

< link rel="stylesheet" href="stylesheet.aspx" >

and that's all for the caller !
Comment permalink 9 Jeremy |
Nice article. Seems to be extremely flexible, but I'm wondering if it is completely so. With the variables stuck in the Web.Config, can you use seperate themes for different domains using the same content?

What if you have an application that is using multiple color schemes based on domain names? How can you adjust for having a green-cyan theme for one domain, an orange-gray theme for another domain and a tan-maroon theme for another domain?

Can you just specify different style sheets to take in the variables that you create in the Web.config? Or would you have to change the way you are specifying the CSS vars?
Comment permalink 10 Milan Negovan |
Jeremy, I haven't dealt with master pages and skins in 2.0 yet. What I've outlined here works for one site because I wrote it when 2.0 wasn't around. I think it's very feasible to implement this on a skin-by-skin basis, though.
Comment permalink 11 sharad singh |
Hi all
This Article is good, but this is not sufficient for me. I'm new to ASP.NET. i understood the code but i didn't understand how to make a request to the CSS from your .aspx file. so i put a link to that CSS in HTML of my page. yeah that started to work, but when you change your color next time, it doesn't change, i figured it out, that its because it requires to refresh the cache. which is not mentioned in this code. it really dont serve my purpose. another thing which i needed is that,that user can change CSS at runtime through httphandler. i want that user of the site can change the css via httphandler at runtime. can someone help me out in this?
Comment permalink 12 David |
How come after you set the SuppressContent variable to true, you don't immediately end the request?

if (modifiedSince != null)
{
DateTime dtLastModified = Convert.ToDateTime (fi.LastWriteTimeUtc.ToString ());
DateTime dtModifiedSince = Convert.ToDateTime (modifiedSince);

if (dtLastModified == dtModifiedSince)
{
// The file has not changed
ctx.Response.StatusCode = 304;
ctx.Response.SuppressContent = true;
}
}

sr = fi.OpenText ();
css = sr.ReadToEnd ();
sr.Close ();
Comment permalink 13 Milan Negovan |
There are a couple of ways to end a request. I believe the safest way is to call HttpApplication.CompleteRequest (). I simply didn't see need to interfere with the page life cycle.
Comment permalink 14 Priya |
I set the context.user to my custom user class in the Global_AcquireRequestState event. This seems to interfere with the ProcessRequest in the HTTPHandler and the CSS is not generated correctly. Everything works great if I remove the line of code that sets the context.user. Any ideas on how I can overcome this behavior? Any help would be greatly appreciated!
Comment permalink 15 Larry |
I always read these types of articles with relish, until I get to the inevitable end - "now we just have to configure IIS" - or something like that. I don't know how many new controls and methods that come with asp.net 2.0 that are completely useless because my site is hosted on a server I have no control over. I've already encountered two or three of them. No hosting company is going to make special allowances for each client.

Sigh...
Comment permalink 16 Milan Negovan |
Larry, I feel your pain. When it comes to configuring IIS, you have to jump through hoops to get the hosting company to cooperate.

Ask them. What's described in the article is quite a trivial task and it's their job to help you.

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):