ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

RemoveCssClass

Removes a css class from the webcontrol. Let's say you have a webcontrol (a label for example) with more than one css class: "defaultClass loggedIn". With the RemoveCssClass extension method, you can easily remove one of them.

Source

public static void RemoveCssClass(this WebControl control, string cssClass)
{
	var classes = from c in control.CssClass.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
				  where !c.Equals(cssClass, StringComparison.OrdinalIgnoreCase)
				  select c;

	control.CssClass = String.Join(" ", classes);
}

Example

ASP.NET Markup:
ASP.NET Markup:
<asp:Label ID="MyLabel" runat="server" CssClass="defaultClass loggedIn" />

C# Codebehind:
if (!userIsLoggedIn)
{
    MyLabel.RemoveCssClass("loggedIn");
}

Author: Kristof Claes

Submitted on: 11 aug. 2010

Language: C#

Type: System.Web.UI.WebControls

Views: 4946