Utility for Painless Month Dropdowns in .NET

Summary Haiku

  • Make binding dropdowns
  • Quick and painless with this neat
  • utility code

I’m a fan of having a utility class in my .NET solutionsto handle the bits of repeated functionality present throughout the projects. Today I found myself needing several day/month/year dropdowns in different controls, and decided I’d throw together a utility to return the months for me as numeric values, full month names, or abbreviated month names. It’s not rocket science, but someone may find this handy.

Note: the code below is assumed to be contained in a static class called Utilities, that I’ll call later from the .ASPX page.

Note part deux: please forgive the lack of indentation and carriage returns, Wordpress is not playing nicely, and I don’t have an opportunity right now to trick it in to doing what I want. Will fix soon.

Creating the utility function

Because I want to toggle between numeric, full and abbreviated month values, I first need an Enumeration:

public enum MonthListType : int {
MonthNumbers,
FullMonthNames,
AbbreviatedMonthNames
}

Now for a function that will return the months in the format I want:

public static Dictionary<string, string> GetMonthList(MonthListType type)
{
Dictionary<string, string> monthList = new Dictionary<string, string>()
{
{ "01", "January"},
{ "02", "February"},
{ "03", "March"},
{ "04", "April"},
{ "05", "May"},
{ "06", "Jun"},
{ "07", "July"},
{ "08", "August"},
{ "09", "September"},
{ "10", "October"},
{ "11", "November"},
{ "12", "Dec"}
}
switch (type)
{
case MonthListType.MonthNumbers:
return monthList.ToDictionary(m => m.Key, m => m.Key);
break;
case MonthListType.FullMonthNames:
return monthList.ToDictionary(m => m.Key, m => m.Value);
break;
case MonthListType.AbbreviatedMonthNames:
return monthList.ToDictionary(m => m.Key, m => m.Value.Substring(0, 3));
break;
default:
return monthList;
break;
}
}

Putting our new code to use

Now in a .ASPX page we’ll add a dropdown :

<asp:DropDownList ID="MonthDropdown" runat="server"></asp:DropDownList>

And in the backend wire it up with our months, with the text values being abbreviated month names:

MonthDropdown.DataSource = Utilities.GetMonthList(Utilities.MonthListType.AbbreviatedMonthNames);
MonthDropdown.DataValueField = "key";
MonthDropdown.DataTextField = "value";
MonthDropdown.DataBind();

Bonus - Painless Day and Year Dropdowns

To quickly generate values 1-31 for our day dropdowns and some reasonable values for our year dropdowns, we can use the handy function Enumerable.Range(start, count) which returns an enumerable list of consecutive integers starting at the integer you specify, and continuing until you have the amount of integers you want. This is easy to apply for the dropdowns:

DayDropdown.DataSource = Enumerable.Range(1, 31);
DayDropdown.DataBind();
YearDropdown.DataSource = Enumerable.Range(DateTime.Now.Year, 10);
YearDropdown.DataBind();

Piece of cake! Leave me a note if you find this useful or have any suggestions to improve it.

Jeff Sargent is a .NET web developer and designer. He loves the Web and tries to make it better through usable, accessible design.

Get the RSS feed | About Jeff | Get in touch

Leave a Comment