Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello friends,
I am creating one website for which i want to change css element value dynamically through c# or vb. how do i change it?
ex. i want to change header back-color through c# or vb.
Posted

There are many way to use CSS dynamiclay from C#.Net Code

at below link I show a very simple way to use it

http://hemantrautela.blogspot.in/2012/09/manage-css-using-cnet-aspnet-code-at.html[^]
 
Share this answer
 
If you want to change the CSS element at runtime through server side code then you need to convert that control into server control by adding 'runat=server' OR inject JavaScript from code behind to change the style.

Option #1: You define the header as server control and provide ID to the header
You can directly, access the CssClass property of the control and set to desired one

Option #2: You inject the JavaScript into page from code behind
You can do it using:
MSDN: RegisterClientScriptBlock Method[^] OR
MSDN: RegisterStartupScript Method[^]

In the JS method, find the header element and access it's style-backcolor attribute to modify it.

Try!
 
Share this answer
 
There are several ways we do,

Simplest way
textBox1.Attributes["class"] = "class name";
//textBox1 is a Text Box Server Control and "class name" is your custom class


Another way is to use Web Extension,
static class WebControlsExtensions
 {
     public static void AddCssClass (this WebControl control, string cssClass)
     {
         control.CssClass += " " + cssClass;
     }
     public static void RemoveCssClass (this WebControl control, string cssClass)
     {
         control.CssClass = control.CssClass.replace(" " + cssClass, "");
     }
 }

ctl.AddCssClass("ReadOnly");
ctl.RemoveCssClass("ReadOnly");

Hope this helps.
cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900