Click here to Skip to main content
Rate this: bad
good
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 20: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
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 13 Sep 2012
Copyright © CodeProject, 1999-2014
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