Handy Drop Down List Utilities
June 4th, 2009 — Brian FooteUsing DropDownLists is so common I’ve created a few utilities you may find useful.
The general pattern is to add a method to your classes which returns a dictionary<string, string> of key and value pairs and then use that method to fill dropdownlists on your page, set defaults, select values etc.
Here is an example method FindKeyItems from a class called Location that creates a dictionary using the classes database key LocationID as the dropdown list value and Description as the text to display.
/// <summary>
/// Returns list of Location key value (LocationID,Description) pairs as a dictionary object
/// </summary>
/// <returns></returns>
public static Dictionary<string, string> FindKeyItems()
{
Database D = DatabaseFactory.CreateDatabase(“CIMS”);
string sql = “Select LocationID, Description from tblLocation order by LocationID”;
DbCommand cmd = D.GetSqlStringCommand(sql);
IDataReader IDR = D.ExecuteReader(cmd);
Dictionary<string, string> Dict = new Dictionary<string, string>();
while (IDR.Read())
{
Dict.Add(IDR.GetValue(0).ToString(), IDR.GetValue(1).ToString());
}
return Dict;
}
The following method populates a dropdown list:
/// <summary>
/// Populates a dropdown list
/// </summary>
/// <param name=”D”>Dropdown list to populate</param>
/// <param name=”Dict”>Dictionary of key/value pairs</param>
/// <param name=”DefaultText”>Text to insert as first item e.g. SELECT ONE. Enter empty string to ignore</param>
/// <param name=”SelectedValue”>ID/Key of item to select by default. Enter empty string to ignore</param>
public static void FillDropDownList(DropDownList D, Dictionary<string, string> Dict, string DefaultText, string SelectedValue)
{
D.Items.Clear();
if (DefaultText != “”) { D.Items.Add(new ListItem(DefaultText, “0″)); }
foreach (KeyValuePair<string, string> KVP in Dict)
{
D.Items.Add(new ListItem(KVP.Value, KVP.Key));
}
ListItem LI = D.Items.FindByValue(SelectedValue);
if (LI != null) { D.Items.FindByValue(SelectedValue).Selected = true; }
}
This overloaded method populates a dropdownlist and sets the default selected item using the text value instead:
/// <summary>
/// Populates a dropdown box and selects an item by text value (instead of ID)
/// </summary>
/// <param name=”D”>Dropdown list to populate</param>
/// <param name=”Dict”>Dictionary of key/value pairs</param>
/// <param name=”DefaultText”>Text to insert as first item e.g. SELECT ONE. Enter empty string to ignore</param>
/// <param name=”SelectedText”>Text value of item to select once populated</param>
public static void FillDropDownListAndSelectItemByText(DropDownList D, Dictionary<string, string> Dict, string DefaultText, string SelectedText)
{
FillDropDownList(D, Dict, DefaultText, “”);
dropDownSelectItemByText(D, SelectedText);
}
Here is the code for dropDownSelectItemByText:
/// <summary>
/// Selects Item in dropdown matching selected text. Does nothing when item not found.
/// </summary>
/// <param name=”D”></param>
/// <param name=”SelectedValue”></param>
public static void dropDownSelectItemByText(DropDownList D, string SelectedText)
{
ListItem LI = new ListItem();
LI = D.Items.FindByText(SelectedText);
if (LI != null)
{
D.SelectedItem.Selected = false;
D.Items.FindByText(SelectedText).Selected = true;
}
}
Here is a method for finding an item by value:
/// <summary>
/// Selects Item in dropdown matching selected value. Does nothing when item not found.
/// </summary>
/// <param name=”D”></param>
/// <param name=”SelectedValue”></param>
public static void dropDownSelectItemByValue(DropDownList D, string SelectedValue)
{
ListItem LI = new ListItem();
LI = D.Items.FindByValue(SelectedValue);
if (LI != null)
{
D.SelectedItem.Selected = false;
D.Items.FindByValue(SelectedValue).Selected = true; }} Here is an example for a TRUE/FALSE list, in case you’re too lazy to just type it into the page <grin>:
public static void FillTrueFalseDropDownList(DropDownList D, string DefaultText, string SelectedValue)
{
Dictionary<string, string> Dict = new Dictionary<string, string>();
Dict.Add(“True”, “True”);
Dict.Add(“False”, “False”);
FillDropDownList(D, Dict, DefaultText, SelectedValue);
}
That completes my list of dropDownList utilities!
- The Guru