Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net

vb.net

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. I know in C# you can use:

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

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

Any ideas anyone?

like image 819
Mark Sandman Avatar asked May 03 '11 14:05

Mark Sandman


People also ask

How do I remove a class from HTML in CSS?

The syntax for Removing CSS classes to an element:removeClass(class_name);

How do you remove a class from an element in HTML?

To remove a class from an element, you use the remove() method of the classList property of the element.

How do I remove all CSS from a class?

To remove all CSS classes of an element, we use removeClass() method. The removeClass() method is used to remove one or more class names from the selected element.

How do I remove a class style?

Remove a class style from a selection Select the object or text you want to remove the style from. In the HTML Property inspector (Window > Properties), select None from the Class pop‑up menu.


2 Answers

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.Remove("class")

C#

mydiv.Attributes.Remove("class");
like image 66
DreamTeK Avatar answered Oct 08 '22 17:10

DreamTeK


This will remove all CSS classes from the div with ID="mydiv"

Me.mydiv.Attributes("class") = ""
like image 25
Mike Sav Avatar answered Oct 08 '22 16:10

Mike Sav