I’m really starting to appreciate extension methods. They provide an easy way to perform small common tasks you would usually put in static utility classes. Below you can find some I use regularly. The first two are extension methods to DateTime. The last two are for the WebControl class. This is the base class for a lot of, well, WebControls in ASP.NET.
When building a website or a web application, letting users enter a date is a common scenario. Often you have to check if the date is between a minimum and a maximum date. When storing a date entered by a user in MS SQL Server, you actually have to perform such a check, because MS SQL Server can’t store DateTime values smaller than 1/1/1753. To make this easier, I have come up with these two extension methods.
namespace System
{
public static class ExtensionMethods
{
public static bool IsBetween(this DateTime date, DateTime firstDate, DateTime secondDate)
{
return (date >= firstDate && date <= secondDate) || (date <= firstDate && date >= secondDate);
}
public static bool IsValidSqlDateTime(this DateTime date)
{
return date.IsBetween((DateTime)System.Data.SqlTypes.SqlDateTime.MinValue, (DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue);
}
}
}
These can be used like this:
DateTime dateToCheck = new DateTime(1492, 10, 12); DateTime firstDate = new DateTime(1400, 1, 1); DateTime secondDate = DateTime.Now; bool isWithinRange = dateToCheck.IsBetween(firstDate, secondDate); bool isValidSqlDateTime = dateToCheck.IsValidSqlDateTime();
The next two extension methods concern the WebControl class. A few examples of controls that inherit from this class are HyperLink, TextBox, Label and Panel. The WebControl base class provides all these controls with the CssClass property. You can assign a string to this property and that string will be rendered as the class attribute of the HTML tag. Now, a lot of the times when you use the CssClass property, you’re not assigning just one class. You’ll often be assigning multiple classes and in a lot of cases you’ll be assigning different classes based on certain conditions. This is not that handy when you have also assigned a default class in the ASP.NET markup:
<asp:Label ID="MyLabel" runat="server" CssClass="defaultClass" />
Let’s say you want to add different classes to that based on whether the current user is logged in or not. To do that, you would have to repeat the defaultClass like this:
if (userIsLoggedIn)
{
MyLabel.CssClass = "defaultClass loggedIn";
}
else
{
MyLabel.CssClass = "defaultClass anonymous";
}
I don’t really like that. That’s why I wrote these extension methods.
namespace System.Web.UI.WebControls
{
public static class ExtensionMethods
{
public static void AddCssClass(this WebControl control, string cssClass)
{
control.CssClass += " " + cssClass;
}
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);
}
}
}
You can use them like this:
if (userIsLoggedIn)
{
MyLabel.AddCssClass("loggedIn");
}
else
{
MyLabel.AddCssClass("anonymous");
}
if (specialCase)
{
MyLabel.RemoveCssClass("defaultClass");
}
I hope this is useful to you!
I really like what you did with css class extension methods. It reminds me of jquery but then for asp.net
very nice!