dynamically change css attribute's value in asp.net

Nauman picture Nauman · Jul 15, 2009 · Viewed 21.9k times · Source

how can i change the css attribute value at runtime like all h1 color="blue" and all p color="green". if anyone know it please help me!

Answer

RichardOD picture RichardOD · Jul 15, 2009

Pretty easy (I'm assuming you want to do this server side)- declare the heading as runat=server and give it an id:

<h1 runat="server" id="someHeading">Blah</h1>

Then you can programmatically manipulate it in the code behind like so:

someHeading.Style.Add("color", "blue");

This will render the following HTML:

<h1 id="someHeading" style="color:blue;">Blah</h1>

You might want do this different and define the styles in a stylesheet and change the class dynamically at runtime.

Update- as you are changing it dynamically, the following might be more appropriate:

someHeading.Style["color"] = "red";

I'm sure someone else will post an answer with how to do it client side. :-)

Another (perhaps better) option to look into server side is using skins and themes. Read the ASP.NET FAQ on these to find out more.