Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove css class in code behind

Tags:

c#

css

asp.net

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?

like image 414
rob waminal Avatar asked Dec 02 '10 09:12

rob waminal


People also ask

How do you remove a class in CSS?

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

How do I remove a class from a tag?

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


2 Answers

You can replace "required" with an empty string:

lblName.CssClass = lblName.CssClass.Replace("required", ""); 
like image 113
jpiolho Avatar answered Sep 28 '22 12:09

jpiolho


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()); } 
like image 25
KevD Avatar answered Sep 28 '22 13:09

KevD