DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

Add css class dynamically from code-behind file for HTML/ASP.NET controls

By Ashok Nalam on 17 Feb 2014 | Category: ASP.NET | Tagged: css class C# 
  Discuss Add Comment   |  
Add rating Rate this resource   
The tip shows how to assign CSS class dynamically through ASP.NET code behind file for HTML controls and ASP.NET controls.

Sample CSS:
.clsbgcolor
{
  background-color:red;
}

HTML controls:

We can't access HTML controls directly in code behind file. So in order to access HTML control add  runat="server" tag to HTML code.
<div id="mydiv" runat="server"></div>
In code behind assign CSS class to div control
protected void Page_Load(object sender, EventArgs e)
{
     mydiv.Attributes.Add("class", "clsbgcolor");
}

ASP.NET controls:

For ASP.NET server side controls directly we can use cssClass property.
<asp:panel runat="server" id="pnl"></asp:panel>
protected void Page_Load(object sender, EventArgs e)
{
    pnl.CssClass = "clsbgcolor"
}
Note: If you are using external CSS file make sure that you have added reference to page.
  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 7003  (Expert)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (4)
 
1. By Stephen on 10 Jun 2019

How many days will it take for the cheque to clear? http://9taxi.in.net/ 9taxi * Management consulting firm Accenture Plc is intalks to acquire rival Booz & Co, a deal that would beef up itsstrategy and operations consulting services. It isn't clear ifthey will reach a deal or if Booz has other suitors. ()

 
2. By Steve on 10 Jun 2019

I really like swimming http://9taxi.in.net/ 9 taxi The defendants, both from Dubai, said that when they boughtthe call options, they had no material, non-public informationthat biotechnology company Amgen Inc was trying to buyits smaller rival for $10 billion, a hefty premium at the time.

 
3. By Melvin on 13 Jun 2019

Go travelling http://xnxx.promo/ xnxx indonesia â??Spring/summer 2013 was a standout season,â? BurberryChief Executive Officer Angela Ahrendts said in the statement.â??The macro outlook remains uncertain and we will continue tofocus our investment on profitable high-growth opportunities bychannel, region and product categories.â?

 
4. By Elmer on 14 Jun 2019

I love the theatre http://xnxx.promo/ xnxxn The new tablets, one with a 7-inch (18cm) screen and one with an 8.9-inch screen, are lighter and more powerful than the last Kindle HD line and appear to be aimed at Apple's iPad. Amazon's new HDX tablets come in 16GB, 32GB and 64GB storage sizes.