Remove a CSS class from HTML element in the code behind file

后端 未结 7 2149
清歌不尽
清歌不尽 2021-02-19 03:15

I have the following on my interface / webform:

Now I have a condition in my

相关标签:
7条回答
  • 2021-02-19 03:31

    I find it better to use REGEX replace since replacing class could exist as partial name in other classes which would break them. So I'm using the following extension method:

    public static void RemoveClass(this WebControl control, string classToRemove)
    {
        if (control == null)
            return;
    
        control.CssClass = Regex.Replace(control.CssClass, @"(^|\s)" + classToRemove + @"($|\s)", " ");
    }
    
    0 讨论(0)
  • 2021-02-19 03:40

    How to remove ONE CSS class from a control using .NET

    If a control has several classes you can remove one of those classes by editing the class string. Both of these methods require assigning an ID to the HTML element so that you can target it in the code behind.

    <asp:Panel ID="mydiv" CssClass="forceHeight class2 class3" runat="server" />
    

    VB.NET

    mydiv.CssClass = mydiv.CssClass.Replace("forceHeight", "").Trim()
    

    C#

    mydiv.CssClass = mydiv.CssClass.Replace("forceHeight", "").Trim();
    

    OR using html generic control

    <div id="mydiv" class="forceHeight class2 class3" runat="server" />
    

    VB.NET

    mydiv.Attributes("class") = mydiv.Attributes("class").Replace("forceHeight", "").Trim()
    

    C#

    mydiv.Attributes["class"] = mydiv.Attributes["class"].Replace("forceHeight", "").Trim();
    

    Optional Trim to remove trailing white space.


    How to remove ALL CSS classes from a control using .NET

    VB.NET

    mydiv.Attributes("class") = ""

    C#

    mydiv.Attributes["class"] = "";

    0 讨论(0)
  • 2021-02-19 03:41
    Me.mydiv.Attributes.Remove("class")
    

    is much better since it won't leave a stub behind. It'll produce a cleaner HTML tag.

    <div id="mydiv"></div>
    

    If you use this,

    Me.mydiv.Attributes("class") = ""
    

    it will produce this instead

    <div id="mydiv" class=""></div> OR <div id="mydiv" class></div>

    0 讨论(0)
  • 2021-02-19 03:43
    mydiv.Attributes["class"] = mydiv.Attributes["class"].ToString().Replace("ClassName","")
    
    0 讨论(0)
  • 2021-02-19 03:45
    public static void appendCssClass(HtmlGenericControl htmlGenericControl, string cssClassName)
        {
            htmlGenericControl.Attributes.Add("class", 
                htmlGenericControl.Attributes["class"].ToString() + " " + cssClassName);
        }
    
        public static void removeCssClass(HtmlGenericControl htmlGenericControl, string cssClassName)
        {
            htmlGenericControl.Attributes.Add("class",
                        htmlGenericControl.Attributes["class"].Replace(cssClassName, ""));
        }
    
    0 讨论(0)
  • 2021-02-19 03:52
    mydiv.Attributes("class") = mydiv.Attributes("class").Replace("forceHeight", "")
    

    The other answers did not work for me. Like the OP said, I'm not getting myDiv.CssClass in IntelliSense.

    0 讨论(0)
提交回复
热议问题