42

I have this control

<asp:Label ID="lblName" runat="server" Text="My Name" CssClass="required regular" />

I want to remove the required class from code behind, how can I do that?

rob waminal
  • 18,117
  • 17
  • 50
  • 64

8 Answers8

71

You can replace "required" with an empty string:

lblName.CssClass = lblName.CssClass.Replace("required", "");
jpiolho
  • 1,192
  • 11
  • 12
  • 18
    Simple, and likely error prone. Say you have a CSS class list like `"dialog authentication-dialog modal"` and you remove the "dialog" class. Using the code from this answer you end up with `" authentication- modal"`. – FreeAsInBeer Jun 25 '14 at 13:10
  • 1
    You can still do a replace, but use regex to exclude the hyphen error case. You can't use the \b word boundary metacharacter, since this will match "-" as a boundary. Instead use something like `lblName.CssClass = Regex.Replace(lblName.CssClass, @"(?<![\w-])dialog(?![\w-])", "");`which uses look behind and look ahead to match word boundaries excluding hyphens – Craig Waddington Apr 07 '17 at 10:17
15

Just a slightly more generic way of doing the same - should rule out potential errors where a css class might occur elsewhere in the CssClass property.

public void RemoveCssClass(WebControl controlInstance, String css)
{
    controlInstance.CssClass = String.Join(" ", controlInstance.CssClass.Split(' ').Where(x => x != css).ToArray());
}
KevD
  • 697
  • 9
  • 17
  • 1
    I prefer this method because it is more like MVC style when you write it into an extension class (providing add and remove CSS class methods on all WebControls). – Tony Wall Apr 09 '13 at 15:17
  • Thanks |I used it and made extension class as below: public static class WebControlExtension { public static void RemoveCssClass(this WebControl controlInstance, String css) { controlInstance.CssClass = String.Join(" ", controlInstance.CssClass.Split(' ').Where(x => x != css).ToArray()); } public static void AddCssClass(this WebControl controlInstance, String css) { controlInstance.CssClass = String.Join($" {css} ", controlInstance.CssClass.Split(' ').ToArray()); } } – Kasim Husaini Aug 06 '18 at 03:32
2

This worked for me

lblName.CssClass = "regular";
Josh Lowe
  • 486
  • 3
  • 13
1

Use this:

object.CssClass= object.CssClass.Replace("MyClass", "");
Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51
Carls Jr.
  • 3,088
  • 7
  • 37
  • 57
  • 1
    ooppsss I actually have the same answer with Jorge M. You can mark his answer as correct. Did not notice though when I posted my answer his answer came first. Sorry about that. – Carls Jr. Dec 02 '10 at 09:57
1

Here is my code inspired by @KevD's code sample.

   public static void RemoveCssClass(this HtmlGenericControl controlInstance, string css)
    {
        var strCssClass = controlInstance.Attributes["class"];
        controlInstance.Attributes["class"] = string.Join(" ", strCssClass.Split(' ').Where(x => x != css).ToArray().Distinct());
    }

    public static void AddCssClass(this HtmlGenericControl controlInstance, string css)
    {
        var strCssClass = controlInstance.Attributes["class"];
        var cssList = strCssClass.Split(' ').ToArray().Distinct();
       cssList= cssList.Append(css);
        controlInstance.Attributes["class"] = string.Join(" ", cssList);
    }

    /// <summary>
    /// Add or remove specific css class
    /// </summary>
    /// <param name="controlInstance">Control to which css is to be added or remove</param>
    /// <param name="css">            Css class name to be added</param>
    /// <param name="bAddClass">      True to Add / false to remove</param>
    public static void AddOrRemoveCssClass(this HtmlGenericControl controlInstance, string css, bool bAddClass)
    {
        if (bAddClass)
        {
            controlInstance.AddCssClass(css);
        }
        else
        {
            controlInstance.RemoveCssClass(css);
        }
    }

    public static void RemoveCssClass(this WebControl controlInstance, string css)
    {
        controlInstance.CssClass = string.Join(" ", controlInstance.CssClass.Split(' ').Where(x => x != css).ToArray().Distinct());
    }

    public static void AddCssClass(this WebControl controlInstance, string css)
    {
        var cssList = controlInstance.CssClass.Split(' ').ToArray().Distinct();
      cssList=  cssList.Append(css);
        controlInstance.CssClass = string.Join(" ", cssList);
    }

    /// <summary>
    /// Add or remove specific css class
    /// </summary>
    /// <param name="controlInstance">Control to which css is to be added or remove</param>
    /// <param name="css">            Css class name to be added</param>
    /// <param name="bAddClass">      True to Add / false to remove</param>
    public static void AddOrRemoveCssClass(this WebControl controlInstance, string css, bool bAddClass)
    {
        if (bAddClass)
        {
            controlInstance.AddCssClass(css);
        }
        else
        {
            controlInstance.RemoveCssClass(css);
        }
    }
Kasim Husaini
  • 392
  • 3
  • 14
0
lblName.Attributes.Add("class","urclassname"); //add class to lblName
dizad87
  • 448
  • 4
  • 15
angfreak
  • 993
  • 2
  • 11
  • 27
  • While this does add the class... it doesn't have a nice "remove" approach like jQuery does... so I'd prefer the CssClass approach like KevD or Jorge outlined. – Eric Burdo May 01 '14 at 20:33
  • Please explain what you did editing your answer, avoid only code answer – GGO Feb 28 '18 at 08:30
0

NOTE: whether you add or replace a css class in codeBehind, remember to include equivalent attributes in both classes i.e. both having background-color, font-family...etc. because otherwise you may be fooled to think that the class never switched even though it did but didn't update the equivalent attributes.

dizad87
  • 448
  • 4
  • 15
0

To remove css Class from Code Behind

lblName.Attributes["class"]=" ";
Martin
  • 2,411
  • 11
  • 28
  • 30
Abhishek Kanrar
  • 418
  • 4
  • 6