I had a cool question while I was at MIX.

A developer needed the ability to have his site render pages using a CSS file selected based on some user specific criteria.

ASP.NET 4 controls generate CSS friendly output and more and more we web developers are using CSS for layout etc.

Using multiple CSS files in our site wide templates we can not only provide different aesthetic experiences but we can charge the style based on the device type (Printer or Phone) or the special needs of the end user (Color Blind of Vision Impaired).

It’s really easy.

I set the Theme Directory yin the Session Start Event Handler.

Though I’, simply setting to a static value here, this is were you would make some decisions in code. You could check for a cookie or the device type.

If you want to make the CSS user selectable you might use a default CSS file until the user logs in and then set this Session Object in the Log In event handler.

protected void Session_Start(object sender, EventArgs e)
Session["Theme"] = "Default";

Make sure your login never fails to set this value. If you’re depending on a Profile setting or Cookie Value, make sure you have a default setting do that it’s never empty.

Then you can just reference it dynamically in the Style Sheet inclusion of your Mast Page file like this.


<link href="/Styles/<%= Session["Theme"] %>/Site.css" rel="stylesheet" type="text/css" />

Simple, but useful.