How to bind List to List box and dropdown list



This tutorial will explain how to bind List<T> to the listbox/dropdown list. Following are some simple steps to bind your listbox/dropdown list to List<T>.

Step 1

Create a List<T> in you code behind file i.e. .CS file. Following code shows how the List<T> of string is created. Months is a List<String> which stores a list of months in a year.

///Creating List of string for months.
    public static List<string> Months = new List<string> { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

Step 2

After creating List<T>, bind List<T> with your listbox/dropdown list. The following code binds to dropdown and listbox too.

//Binding list of string to listbox
        lst.DataSource = Months;
        lst.DataBind();

        //Binding list of string to dropdown list
        drp.DataSource = Months;
        drp.DataBind();


Output:

When you run your application, the List<T> is bound to the listbox/dropdown list.

ListBox.gif

Up Next
    Ebook Download
    View all
    Learn
    View all