by Rod
12. March 2010 13:40
A student/colleague of mine, after checking out a site I did, asked me: how do you change CSS files dynamically?
The answer is very simple;
I’m using a master page here and usually in the head container there’d be your css link;
By adding ‘runat=”server”’ and giving it a unique id you make the link run server side, and then after that modifying the ‘href’ attribute is done in the code behind.
<link id="masterCss" href="App_Themes/Theme1/yourcssfile.css" type="text/css" rel="stylesheet" runat="server" />
Then in the code behind :
masterCss.Href = "App_Themes/Theme1/secondcssfile.css";
Clearly that would be best set up in : “Page_Load” event or “Page_PreRender” depending on your needs :).