I was doing a fair bit of work with drop down lists in ASP .NET the other day and seemed to be constantly writing code to select items in the list, then I realised that this code would be ideal for some extension methods. So I came up with the following:
public static class DropDownListExtension
{
/// <summary>
/// Populates the specified drop down list with the names of the specified enum type.
/// </summary>
/// <param name="dropDownList">The drop down list to populate.</param>
/// <param name="enumType">Type of the enum.</param>
public static void PopulateWithEnum<T>(this DropDownList dropDownList)
{
DropDownListExtension.PopulateWithEnum<T>(dropDownList, true, true);
}
/// <summary>
/// Populates the specified drop down list with the names of the specified enum type.
/// </summary>
/// <param name="dropDownList">The drop down list to populate.</param>
/// <param name="enumType">Type of the enum.</param>
/// <param name="clearItems">if set to <c>true</c> clear any existing items in the list.</param>
/// <param name="insertSpaces">if set to <c>true</c> insert spaces between capital letters for readability in UI.</param>
public static void PopulateWithEnum<T>(this DropDownList dropDownList, bool clearItems, bool insertSpaces)
{
if (clearItems)
{
dropDownList.Items.Clear();
}
foreach (var name in Enum.GetNames(typeof(T)))
{
ListItem item = new ListItem()
{
Text = insertSpaces ? Format.SplitPascalCase(name) : name,
Value = name
};
dropDownList.Items.Add(item);
}
}
/// <summary>
/// Gets the selected value and parses it as the specified enum type.
/// </summary>
/// <typeparam name="T">The type of enum to return.</typeparam>
/// <param name="dropDownList">The drop down list.</param>
/// <returns>An enum value of the specified type.</returns>
public static T GetSelectedValueAsEnum<T>(this DropDownList dropDownList)
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException(string.Format("{0} is not a valid enum.", typeof(T).Name));
}
return (T)Enum.Parse(typeof(T), dropDownList.SelectedValue);
}
/// <summary>
/// Sets the selected item by finding the specified value in the drop down list.
/// </summary>
/// <typeparam name="T">The type of the value to set.</typeparam>
/// <param name="dropDownList">The drop down list.</param>
/// <param name="value">The value.</param>
/// <returns>
/// <c>true</c> if the data selection was successful; otherwise <c>false</c>
/// </returns>
public static bool SetSelectedValue<T>(this DropDownList dropDownList, T value)
{
return SetSelectedValue<T>(dropDownList, value, CultureInfo.CurrentUICulture);
}
/// <summary>
/// Sets the selected item by finding the specified value in the drop down list.
/// </summary>
/// <typeparam name="T">The type of the value to set.</typeparam>
/// <param name="dropDownList">The drop down list.</param>
/// <param name="value">The value.</param>
/// <param name="culture">The culture to format the string to search by.</param>
/// <returns>
/// <c>true</c> if the data selection was successful; otherwise <c>false</c>
/// </returns>
public static bool SetSelectedValue<T>(this DropDownList dropDownList, T value, CultureInfo culture)
{
bool success = false;
if (value != null)
{
ListItem item = dropDownList.Items.FindByValue(string.Format(culture, "{0}", value));
if (item != null)
{
dropDownList.SelectedIndex = dropDownList.Items.IndexOf(item);
success = true;
}
}
return success;
}
/// <summary>
/// Sets the selected item by finding the specified text value in the drop down list.
/// </summary>
/// <typeparam name="T">The type of the text value to set.</typeparam>
/// <param name="dropDownList">The drop down list.</param>
/// <param name="text">The text to find in the list.</param>
/// <returns><c>true</c> if the data selection was successful; otherwise <c>false</c></returns>
public static bool SetSelectedText<T>(this DropDownList dropDownList, T text)
{
return SetSelectedText<T>(dropDownList, text, CultureInfo.CurrentUICulture);
}
/// <summary>
/// Sets the selected item by finding the specified text value in the drop down list.
/// </summary>
/// <typeparam name="T">The type of the text value to set.</typeparam>
/// <param name="dropDownList">The drop down list.</param>
/// <param name="text">The text.</param>
/// <param name="culture">The culture to format the string to search by.</param>
/// <returns><c>true</c> if the data selection was successful; otherwise <c>false</c></returns>
public static bool SetSelectedText<T>(this DropDownList dropDownList, T text, CultureInfo culture)
{
bool success = false;
if (text != null)
{
ListItem item = dropDownList.Items.FindByText(string.Format(culture, "{0}", text));
if (item != null)
{
dropDownList.SelectedIndex = dropDownList.Items.IndexOf(item);
success = true;
}
}
return success;
}
}
These methods can be simply used to set the selected text in an ASP .NET drop down list or populate a list with the contents of an enum.
Cheers