Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to bind DropDownList and CheckBoxList to Dictionary in ASP.NET

Author: Richard Wand
Download Source Code :

This article will explain how we can bind DropDownList and CheckBoxList to Dictionary in ASP.NET.


In my previous article, I have explained about Dictionary object. I have also explained how we can create Dictionary object and how to add and retrieve items to/from Dictionary object. This simple article will explain how we can bind DropDownList and CheckBoxList to Dictionary in ASP.NET.
  1. Create new website in Visual Studio 2010
  2. Add a DropDownList and a CheckBoxList control in aspx page of a web form

    <h3>Bind DropDownList and CheckBoxList to Dictionary</h3>
    <asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="True"            onselectedindexchanged="ddlCountries_SelectedIndexChanged">
    <br />
    <br />
    <asp:CheckBoxList ID="cblCities" runat="server">
  3. Write code below in page load event of code behind file

    protected void Page_Load(object sender, EventArgs e)
           if (!IsPostBack)
                  Dictionary countries = new Dictionary();
                  countries.Add("-1", "Select Country");
                  countries.Add("US", "United States of America");
                  countries.Add("UK", "United Kingdom");
                  countries.Add("AU", "Australia");
                  ddlCountries.DataSource = countries;
                  ddlCountries.DataTextField = "Value";
                  ddlCountries.DataValueField = "Key";
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
           If Not IsPostBack Then
                  Dim countries As New Dictionary(Of String, String)()
                  countries.Add("-1", "Select Country")
                  countries.Add("US", "United States of America")
                  countries.Add("UK", "United Kingdom")
                  countries.Add("AU", "Australia")
                  ddlCountries.DataSource = countries
                  ddlCountries.DataTextField = "Value"
                  ddlCountries.DataValueField = "Key"
           End If
    End Sub
    In above code, we have created a Dictionary object “countries” and added three items into it as Key/Value pair. At the end, we have provided Dictionary object as data source of DropDownList. The “Value” is set as DataTextField and “Key” is set as DataValueField for DropDownList.
  4. Now write below function in the code file

    public Dictionary GetCities(string country)
           Dictionary cities = new Dictionary();
           if (country == "United States of America")
                  cities.Add(1, "New York");
                  cities.Add(2, "Washington");
                  cities.Add(3, "Seatle");
           else if (country == "United Kingdom")
                  cities.Add(1, "London");
                  cities.Add(2, "Leeds");
                  cities.Add(3, "Manchester");
           else if (country == "Australia")
                  cities.Add(1, "Sydney");
                  cities.Add(2, "Melbourne");
                  cities.Add(3, "Perth");
           return cities;
    Public Function GetCities(country As String) As Dictionary(Of Integer, String)
    Dim cities As New Dictionary(Of Integer, String)()
           If country = "United States of America" Then
                  cities.Add(1, "New York")
                  cities.Add(2, "Washington")
                  cities.Add(3, "Seatle")
           ElseIf country = "United Kingdom" Then
                  cities.Add(1, "London")
                  cities.Add(2, "Leeds")
                  cities.Add(3, "Manchester")
           ElseIf country = "Australia" Then
                  cities.Add(1, "Sydney")
                  cities.Add(2, "Melbourne")
                  cities.Add(3, "Perth")
           End If
           Return cities
    End Function
    This function takes a string argument and returns Dictionary object. In the function, we have created another dictionary object “cities”. We have added cities for every country using If Else statement.
  5. Add SelectedIndexChanged event of DropDownList in the code file


    protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
           cblCities.DataSource = GetCities(ddlCountries.SelectedItem.Text);
           cblCities.DataTextField = "Value";
           cblCities.DataValueField = "Key";

    Protected Sub ddlCountries_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlCountries.SelectedIndexChanged
           cblCities.DataSource = GetCities(ddlCountries.SelectedItem.Text)
           cblCities.DataTextField = "Value"
           cblCities.DataValueField = "Key"
    End Sub
    In SelectedIndexChanged event, we have called the GetCities() function and provided as data source to CheckBoxList. Again “Value” is set as DataTextField and “key” is set as DataValueField for ChechBoxList.

  6. Now start debugging and see the result


Add Article Comment:
Name :
Email Address :
Comments :
<< Dictionary object in ASP.NET

Disclaimer - Privacy
© 2002-2017