简体   繁体   English

从代码隐藏文件中的 HTML 元素中删除 CSS class 元素

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

I have the following on my interface / webform:我的界面/网络表单上有以下内容:

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

Now I have a condition in my code behind where if a certain situation is true I need to remove the forceHeight class from this control.现在我的代码中有一个条件,如果某种情况属实,我需要从此控件中删除forceHeight class 。 I know in C# you can use:我知道在 C# 你可以使用:

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

I'm not so sure how you do this using VB?我不太确定你是如何使用VB做到这一点的? Intellisense doesn't offer me this option? Intellisense 不向我提供此选项?

Any ideas anyone?有什么想法吗?

How to remove ONE CSS class from a control using .NET如何使用 .NET 从控件中删除一个 CSS class

If a control has several classes you can remove one of those classes by editing the class string.如果一个控件有多个类,您可以通过编辑 class 字符串来删除其中一个类。 Both of these methods require assigning an ID to the HTML element so that you can target it in the code behind.这两种方法都需要为 HTML 元素分配一个 ID,以便您可以在后面的代码中定位它。

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

VB.NET VB.NET

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

C# C#

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

OR using html generic control使用html 通用控制

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

VB.NET VB.NET

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

C# C#

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

Optional Trim to remove trailing white space.可选Trim以删除尾随空格。


How to remove ALL CSS classes from a control using .NET如何使用 .NET 从控件中删除所有 CSS 类

VB.NET VB.NET

mydiv.Attributes.Remove("class")

C# C#

mydiv.Attributes.Remove("class");

This will remove all CSS classes from the div with ID="mydiv"这将从ID="mydiv"的 div 中删除所有 CSS 类

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

is much better since it won't leave a stub behind.更好,因为它不会留下存根。 It'll produce a cleaner HTML tag.它将产生一个更清洁的 HTML 标签。

<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> <div id="mydiv" class=""></div><div id="mydiv" class></div>

I find it better to use REGEX replace since replacing class could exist as partial name in other classes which would break them.我发现使用 REGEX 替换更好,因为替换 class 可能作为部分名称存在于其他类中,这会破坏它们。 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)", " ");
}
mydiv.Attributes["class"] = mydiv.Attributes["class"].ToString().Replace("ClassName","")
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 .就像 OP 所说,我没有在IntelliSense中获得myDiv.CssClass

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, ""));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM