Wednesday, September 26, 2007

Binding Data Source for asp DropDownList with Dictionary

While I was trying to bind data source (which is dictionary) I got a way to bind data.

The drop down control in .aspx page




DataBinding in .aspx.cs

Dictionary cityDataList = new Dictionary();
cityDataList.Add("DEL", "Delhi");
cityDataList.Add("BOM", "Mumbai");
cityDataList.Add("MAA", "Chennai");
cityDataList.Add("GOI", "Goa");

// Binding dictionary data with DropDownList
foreach (string key in cityDataList.Keys)
{
CityList.Items.Add(new ListItem(cityDataList[key], key));
}