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

AddCssClass

Adds a css class to the webcontrol. Instead of having to pass one string to the CssClass property, you can add them one by one with the AddCssClass extension method. This can come in handy when a webcontrol has a default class (from the ASP.NET markup) and then needs additional classes based on a condition (like whether or not a user is logged in).

Source

public static void AddCssClass(this WebControl control, string cssClass)
{
    control.CssClass += " " + cssClass;
}

Example

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

C# Codebehind:
if (userIsLoggedIn)
{
    MyLabel.AddCssClass("loggedIn");
}
else
{
    MyLabel.AddCssClass("anonymous");
}

Author: Kristof Claes

Submitted on: 11 aug. 2010

Language: C#

Type: System.Web.UI.WebControls

Views: 6007