Change CSS in backend

18,320

Solution 1

myDiv.Attributes.CssStyle.Add("color", "white");

This is how I do by adding CSS for the same based on the condition.

Solution 2

Apply the CSS class via attributes, like this:

myDiv.Attributes["class"] = "myDivStyle"; 
Share:
18,320
Jaiesh_bhai
Author by

Jaiesh_bhai

Updated on June 14, 2022

Comments

  • Jaiesh_bhai
    Jaiesh_bhai almost 2 years

    When using asp.net, is it possible to modify css properties in the c# back end code in the same as Div tags? I cannot get it to work.

    For example:

    HTML

    <div id="myDiv" runat="server" style="height:10px; color:black;" />
    

    C#

    myDiv.Style.Add("color", "white");
    

    This works, however I am trying to do the same to a css class instead. So if I had: CSS

     .myDivStyle
        {
           height:10px;
           color:black;
        }
    

    How can I access this and change the color in c#? The only effective way I was able to find was to have two css classes and replace the class with another one such as the answer to this: Replacing css class solution

    I have tried:

    myDivStyle.Style.Add("color","white");
    

    I was wondering if it is possible to include the runat="server" property directly to the css class. Thanks in advance.