Click here to Skip to main content
12,406,124 members (76,513 online)
Rate this:
 
Please Sign up or sign in to vote.
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 11-Sep-12 19:56pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160726.1 | Last Updated 13 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100