ASP.NET : Change CSS from code behind.

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 :).

Tags:

asp.net

About the author

Rodrigue Hajjar

.NET Developer.

.Supinfo Student.

 

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar