DropDownList, ListBox, and ComboBox: What's the Difference?

What's the difference between DropDownList, ListBox, and ComboBox controls in ASP.NET?

- PS, Denver, Colo.

All three of these are ways to present data to the user so they can select it, although ComboBox isn't available out of the box. ListBox shows many options to the user simultaneously and lets them pick one or more, whereas DropDownList lets them choose only one.

Let's start with ListBox. This box is scrollable and filled with choices (see Figure 1). You select an item by clicking on it, and you can hold down the Control key while clicking Two important properties that you'll find in Visual Studio .NET's design mode are Rows and SelectionMode. Rows lets you specify how many rows of information should appear; you can define this as any number between one and 2,000 (the default is four). You can set SelectionMode either to Multiple or Single, which determines whether you allow the user to select multiple items from the ListBox.


A ListBox is a container for a collection of ListItems. These ListItems can comprise a dataset bound to the ListBox, or you can generate it manually. For the demo code, I use manually generated data (and I freely admit that I cheated and used the Items property in design mode rather than generating HTML for the items by hand). A quick perusal of the HTML shows the value assigned to a list item is a property of the list item element, and the text displayed appears between the opening and closing spans of the element.

To do something with the selected items, you first need to check if anything has been selected using the SelectedIndex property of the ListBox. A value of -1 signifies that nothing is selected; any other value indicates the first item selected. If you set the SelectionMode property to Single, this value is the index of the only selected item and you can obtain the item's value through the SelectedItem property.

If you set SelectionMode to Multiple, SelectedIndex and SelectedItem will point only to the first item selected, so it's safer to loop through the items, checking each item's Selected property to see if the item is selected (check out the sample code for an example of this procedure; see end of article for download details).

DropDownLists resemble ListBoxes. In fact, I can copy the items directly from the ListBox for use in the DropDownList, but they do differ in some ways. Perhaps the most noticeable difference is that, with DropDownLists, an item must be selected. You won't get a SelectedIndex of -1. To bypass this, I added a blank selection to the data and modified my processing code to see if SelectedIndex equals zero. Another difference is you can't set the quantity of rows that appear when the list drops down. If the list contains less than 11 rows, you will see all the data; if it contains more, you'll see a scrollable box drop down, listing all the options. Because the user can select only one option, using SelectedIndex and SelectedItem is the best way to access the item that has been chosen.

The last thing to mention is the ComboBox control. Unofficially, you can refer to this control as an editable DropDownList that lets you select one of the provided items or type in your own item. ASP.NET doesn't include one, but you can search for one of the several commercially available ComboBox controls to supplement the ASP.NET package.

The sample referenced in this article is available for download.

 

Josef Finsel is a software consultant with G.A. Sullivan, a global software development company. He has published several VB and SQL Server-related articles and is working on the syntax for FizzBin.NET, a programming language that works the way programmers have always suspected.. Send your ASP.NET questions to [email protected]

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish