How to set up a CSS switcher How to set up a CSS switcher asp.net asp.net

How to set up a CSS switcher


In Asp.net 3.5, you should be able to set up the Link tag in the header as a server tag. Then in the codebehind you can set the href property for the link element, based on a cookie value, querystring, date, etc.

In your aspx file:

<head>  <link id="linkStyles" rel="stylesheet" type="text/css" runat="server" /></head>

And in the Code behind:

protected void Page_Load(object sender, EventArgs e) {  string stylesheetAddress = // logic to determine stylesheet  linkStyles.Href = stylesheetAddress;}


You should look into ASP.NET themes, that's exactly what they're used for. They also allow you to skin controls, which means give them a set of default attributes.


I would suggest storing the stylesheet selection in the session so you don't have to rely on the querystring key being present all the time. You can check the session in Page_Load and add the appropriate stylesheet reference. It sounds like this is a temporary/development situation, so go with whatever is easy and works.

if (!String.IsNullOrEmpty(Request.QueryString["css"]))  Session.Add("CSS",Request.QueryString["css"]);