• How to bind dropdownlist to dictionary

    Posted on July 30, 2012 by in ASP.NET, C#, Dotnet

    In this article, I am going to explain how to bind dropdownlist to dictionary.

    Create a simple ASP.NET page with  a drop down list as shown below:

    <head runat="server">
        <form id="form1" runat="server">
        <asp:DropDownList ID="ddlProducts" runat="server"></asp:DropDownList>

    In the Page_Load event, simply bind the dropdownlist with a dictionary object as shown below:

    protected void Page_Load(object sender, EventArgs e)
        //populate a dictionary object with sample data
        var products = new Dictionary<int, string>();
        products[1] = "Adjustable Race";
        products[2] = "Bearing Ball";
        products[3] = "BB Ball Bearing";
        products[4] = "Headset Ball Bearings";
        //Sets the field of the data source that provides the text content
        //of the list items.
        ddlProducts.DataTextField = "Value";
        //Set the field of the data source that provides the value of each
        //list item.
        ddlProducts.DataValueField = "Key";
        //set the data source
        ddlProducts.DataSource = products;            

    Run the page and you would notice that the drop down list is populated with dictionary.
    bind dropdownlist to dictionary asp.net 1

    Be Sociable, Share!
      Post Tagged with , ,

    Written by

    Software architect with over 10 years of proven experience in designing & developing n-tier and web based software applications, for Finance, Telecommunication, Manufacturing, Internet and other Commercial industries. He believes that success depends on one's ability to integrate multiple technologies to solve a simple as well as complicated problem.

    View all articles by

    Email : [email protected]

    Leave a Reply