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.