Previously I’ve posted a few Helper Classes . This post describes my ListControlHelper class
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using System.Data;
public static class ListControlHelper
{
public static string EmptySelectionValue = “-1”;
//’http://blogs.msdn.com/piyush/archive/2006/10/17/how-to-bind-generic-dictionary-with-dropdown-list.aspx
public static void ControlBinding(ListControl ctrl, Dictionary<string, string> dict)
{
ctrl.DataSource = dict;
ctrl.DataValueField = “Key”;
ctrl.DataTextField = “Value”;
}
//Overload for StringDictionary
//NOTE:Do NOT use the class for Case-Sensitive data.
public static void ControlBinding(ListControl ctrl, StringDictionary dict)
{
ctrl.DataSource = dict;
ctrl.DataValueField = “Key”;
ctrl.DataTextField = “Value”;
}
public static void ControlBinding(ListControl ctrl, DataTable tbl,string valueField, string textField)
{
ctrl.DataSource = tbl;
ctrl.DataValueField = valueField;
ctrl.DataTextField = textField;
}
public static void AddRange(ListControl pDDL, string[] asText)
{
foreach(string sText in asText)
{
pDDL.Items.Add(sText);
}
// return pDDL;
}
//Ideas from http://geekswithblogs.net/jawad/archive/2005/06/24/EnumDropDown.aspx#55344
//, http://www.codeproject.com/useritems/enumdatabinding.asp
//and from http://rodenbaugh.net/files/folders/18/download.aspx
public static ListControl PopulateListControlFromEnum<TEnum>(ListControl pDDL,
string selectedValue, EnumPopulation populateType, string emptyText, Predicate<TEnum> match)
{
// pDDL.Items.Clear();
Array values = Enum.GetValues(typeof(TEnum));
Array names = Enum.GetNames(typeof(TEnum));
if (null != emptyText)
{
pDDL.Items.Add(new ListItem(emptyText, “-1”));
}
for (int i = 0; i < values.Length; i++)
{
string key = “”;
string text = “”;
if (match != null)
{
if (false == match((TEnum)values.GetValue(i)))
{
continue;
}
}
switch (populateType)
{
case EnumPopulation.NameToName:
key = names.GetValue(i).ToString();
text = key;
break;
case EnumPopulation.ValueToName://value is numeric
key = Convert.ToInt32(values.GetValue(i)).ToString();
text = names.GetValue(i).ToString();
break;
default:
//case EnumPopulation.NameToDescription :
//case EnumPopulation.ValueToDescription :
Debug.Assert(false, “TODO: implement “);
break;
}
pDDL.Items.Add(new ListItem(text, key));
}
if (selectedValue != “”)
{
pDDL.SelectedValue = selectedValue;
}
return pDDL;
}
public static ListControl PopulateListControl(ListControl pDDL, string[] asText,
string selectedValue, string emptyText)
{
pDDL.Items.Clear();
AddRange(pDDL, asText);
AddEmptyTextAndSelect(pDDL, selectedValue, emptyText);
return pDDL;
}
public static void AddEmptyTextAndSelect(ListControl pDDL, string selectedValue, string emptyText)
{
if (null != emptyText)
{
pDDL.Items.Add(new ListItem(emptyText, “-1”));
}
if (selectedValue != “”)
{
pDDL.SelectedValue = selectedValue;
}
}
//from http://geekswithblogs.net/jawad/archive/2005/06/24/EnumDropDown.aspx#55344
//and http://www.codeproject.com/useritems/enumdatabinding.asp
public static ListControl PopulateListControlFromEnum<TEnum>(ListControl pDDL,
string selectedValue, EnumPopulation populateType, string emptyText)
{
return PopulateListControlFromEnum<TEnum>( pDDL, selectedValue, populateType, emptyText,null);
// pDDL.Items.Clear();
//Array values = Enum.GetValues(enumType);
//Array names = Enum.GetNames(enumType);
//if (null!=emptyText)
//{
// pDDL.Items.Add(new ListItem(emptyText, “-1”)) ;
//}
//for(int i=0;i < values.Length;i++)
//{
// string key =””;
// string text=””;
// switch(populateType)
// {
// case EnumPopulation.NameToName:
// key = names.GetValue(i).ToString();
// text =key;
// break;
// case EnumPopulation.ValueToName: //value is numeric
// key =Convert.ToInt32(values.GetValue(i)).ToString();
// text = names.GetValue(i).ToString();
// break;
// default:
// //case EnumPopulation.NameToDescription :
// //case EnumPopulation.ValueToDescription :
// Debug.Assert(false,”TODO: implement “);
// break;
// }
// pDDL.Items.Add(new ListItem(text, key));
//}
//if (selectedValue != “”)
//{
// pDDL.SelectedValue = selectedValue;
//}
//return pDDL;
}
public static int GetSelectedEnumValue<TEnum>(ListControl pDDL) where TEnum : struct
{
string selectedValue = pDDL.SelectedValue;
if (selectedValue != “”)
{
TEnum enRet = EnumHelper.Parse<TEnum>(selectedValue);
return Convert.ToInt32(enRet);//(int)
}
return -1;
}
public static ListItem ReplaceListItem(ListControl pDDL, string originalEmptyValue, string emptyText, string newEmptyValue)
{
ListItem li=pDDL.Items.FindByValue(originalEmptyValue);
if(li!=null)
{
if (null != emptyText)
{
li.Text = emptyText;
}
if (null != newEmptyValue)
{
li.Value = newEmptyValue;
}
}
return li;
}
public static bool IsItemSelected(DropDownList ddlMonth)
{
if ((ddlMonth == null) || (ddlMonth.SelectedItem == null))
{
return false;
}
return true;
}
public enum EnumPopulation
{
ValueToName,
NameToName,
NameToDescription,
ValueToDescription
}
}// class ListControlHelper