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


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.


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);


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

C# Codebehind:
if (!userIsLoggedIn)

Author: Kristof Claes

Submitted on: 11 aug 2010

Language: C#

Type: System.Web.UI.WebControls

Views: 4120