Cures for the Common Ailments - DropDownList

A very common issue that many new developers come into when using a DropDownList control is how to truly bind items to the list. A common problem that happens is they bind just the items or records from the list. This creates an issue when the user wants to select the first item in the list and there is an event handler for the SelectedIndexChanged event. It is also not a good user experience.

The solution to both of these issues is to insert an item at the top of the list to instruct the user what to do. So the user experience is enhanced because now the user is more aware of what action is expected. But more importantly when you are autoposting back to the server when a selection is made, you know the event will be fired.

The problem is if the user selects the first item in the list, nothing is actually changing. You have to realize you may get users or customers from Alabama! By inserting the instructional item it forces the user to make a real selection when the page is first presented to the user. Therefor the SelectedIndexChanged event will be fired when the user actually changes.

The best way I have found to implement this solution is to insert the instructional item at run-time after the items have been bound to the control. Granted this scenario can be averted if you a adding items declaratively to the DropDownList, but this will be rare and binding to records from a database is more common.

dlState.Items.Insert(0, New Web.UI.WebControls.ListItem("Select State", 0))

I had this problem quite a bit when I first started developing with ASP.NET. I think this is a common issue when developers are just starting to develop web sites and even desktop applications.

Share This Article With Your Friends!

Googles Ads Facebook Pixel Bing Pixel LinkedIn Pixel