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

时间:2022-11-22 23:21:22

I have the following on my interface / webform:

我的界面/ 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. I know in C# you can use:

现在我的代码中有一个条件,如果某种情况属实,我需要从该控件中删除forceHeight类。我知道在C#中你可以使用:

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

I'm not so sure how you do this using VB? Intellisense doesn't offer me this option?

我不太确定你是怎么用VB做的? Intellisense不提供这个选项吗?

Any ideas anyone?

任何人的想法?

5 个解决方案

#1


17  

try this

尝试这个

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

#2


15  

How to remove ONE css class from an object using .NET

If an object has several classes you can remove one of those classes by editing the class string:

如果一个对象有多个类,你可以通过编辑类字符串来删除其中一个类:

<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.CssClass = mydiv.CssClass.Replace("forceHeight", "").Trim()

C#

C#

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

Optional Trim to remove trailing white space.

可选修剪以删除尾随空白区域。


How to remove ALL css classes from an object using .NET

VB.NET

VB.NET

mydiv.Attributes("class") = ""

mydiv.Attributes(“class”)=“”

C#

C#

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

mydiv.Attributes [“class”] =“”;

Will remove the class attribute from the object.

将从对象中删除class属性。

#3


10  

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>

#4


3  

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:

我发现使用REGEX替换更好,因为替换类可能在其他类中作为部分名称存在,这会破坏它们。所以我使用以下扩展方法:

public static void RemoveClass(this WebControl control, string classToRemove)
{
    if (control == null)
        return;

    control.CssClass = Regex.Replace(control.CssClass, @"(^|\s)" + classToRemove + @"($|\s)", " ");
}

#5


0  

mydiv.Attributes["class"] = mydiv.Attributes["class"].ToString().Replace("ClassName","")

#1


17  

try this

尝试这个

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

#2


15  

How to remove ONE css class from an object using .NET

If an object has several classes you can remove one of those classes by editing the class string:

如果一个对象有多个类,你可以通过编辑类字符串来删除其中一个类:

<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.CssClass = mydiv.CssClass.Replace("forceHeight", "").Trim()

C#

C#

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

Optional Trim to remove trailing white space.

可选修剪以删除尾随空白区域。


How to remove ALL css classes from an object using .NET

VB.NET

VB.NET

mydiv.Attributes("class") = ""

mydiv.Attributes(“class”)=“”

C#

C#

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

mydiv.Attributes [“class”] =“”;

Will remove the class attribute from the object.

将从对象中删除class属性。

#3


10  

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>

#4


3  

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:

我发现使用REGEX替换更好,因为替换类可能在其他类中作为部分名称存在,这会破坏它们。所以我使用以下扩展方法:

public static void RemoveClass(this WebControl control, string classToRemove)
{
    if (control == null)
        return;

    control.CssClass = Regex.Replace(control.CssClass, @"(^|\s)" + classToRemove + @"($|\s)", " ");
}

#5


0  

mydiv.Attributes["class"] = mydiv.Attributes["class"].ToString().Replace("ClassName","")