ASP.NET WebControl Critical Analysis: DropDownList

There is a lot of the common functionality when working with html controls that Microsoft simplified (some overcomplicated in my opinion) in order to fit into their WebForms framework.

This is my first post (of hopefully many to follow) where I am going to look at some of the common issues I've seen over the years with regards to webcontrols.

Note that I doubt that Microsoft see them as issues, since they didn't seem to endeavour to fix/improve any of this in the latest incarnation of the product, so all of these issues are likely by intended design.

The first control I am going to look at is the DropDownList webcontrol.

<asp:DropDownList runat="server" ID="ddlLetters">
	<asp:ListItem Text="A" Value="1"></asp:ListItem>
	<asp:ListItem Text="B" Value="2"></asp:ListItem>
	<asp:ListItem Text="C" Value="3" Selected="True"></asp:ListItem>
	<asp:ListItem Text="D" Value="4"></asp:ListItem>

This control is an abstraction Microsoft created for the (X)HTML select list (the single selection part of it at least, they created a separate abstraction for the multiple selection - ListBox).

<select name="letters">
   <option value="1">A</option>
   <option value="2">B</option>
   <option selected="selected" value="3">C</option>
   <option value="4">D</option>


    Probably the most common issue is the fact that the DropDownList doesnt support optgroups.

    Select tag optgroups
    <select name="letters">
    	<optgroup label="Microsoft">
    	   <option value="1">C#</option>
    	   <option value="2"></option>
    	<optgroup label="">
    	   <option value="3">PHP</option>
    	   <option value="4" disabled="disabled">Java</option>
    	   <option value="5">Perl</option>

    The optgroup tag is by no means deprecated or obsolete or anything, so I am not quite sure why they excluded this functionality. Incidently, I wrote a post a while ago on how to include this functionality by means of WebControlAdapters.


    SelectedValue not Browsable

    Not really an issue (but confusing to some) that the SelectedValue is not visible in the designer, the reason being that whoever wrote the ListControl base class (the class from which the DropDownList inherits from) set the Browsable attribute above the property to false.

    I've seen devs coding around this (sometimes with good reason, like seen in point 3) even though it is still possible to bind/eval the SelectedValue when using it within a parent control e.g. ListView, FormView etc.

    <asp:DropDownList runat="server" ID="ddlLetters" Selectedvalue='<%# Eval("somevalue") %>'>		

    I do however believe that the developer(s) "hid" this property on purpose, I believe that the following issue might give us a clue to why (s)he did it.


    Chances are that you've come across the following exception:

    'ddlSomething' has a SelectedValue which is invalid because it does not exist in the list of items

    The reason we get this exception is obvious, our selected value needs to be in the list we're setting it to and its not.

    When working with lists that constantly change (add/removed/edited), or in certain binding scenarios one can easily get this exception but equally as easy to manage this exception in our codebehind/codefile, but it becomes problematic to trap this exception as soon as we're working with something like ObjectDataSources and we attempt to bind the SelectedValue like seen in point 2.

    We get forced to bind our SelectedValue in our DataBound event like seen below (something you can perhaps move to a WebControlAdapter).

    protected void ddlLetters_DataBound(object sender, EventArgs e)
    // Depends on the context in which this event fires
        ((DropDownList)sender).SelectedValue = Convert.ToString(Eval("somevalue"));

    Which means its safer to bind our SelectedValue in the codebehind making it prudent to rather hide the SelectedValue property from the designer.

    It would have probably made more sense to allow developers to specify a default item to which our SelectedValue must revert if its not found and if no default item is specified we simply throw the does not exist exception like normally.

    Note: This workaround won't work for all scenarios, perhaps we can look at other scenarios in a future post.


    It makes sense to only list unique items in our DropDownList, but what happens if we list non-unique values? Meaning our Text/display is unique, but the value associated with it is not e.g:

    <asp:DropDownList runat="server" ID="ddlLetters">
        <asp:ListItem Text="A" Value="2"></asp:ListItem>
        <asp:ListItem Text="B" Value="1"></asp:ListItem>
        <asp:ListItem Text="C" Value="1"></asp:ListItem>
        <asp:ListItem Text="D" Value="4"></asp:ListItem>

    I remember helping someone with this very issue a few years ago (he wasted a good hour trying to figure this out, rather sad).

    Every time someone selected C it would highlight B after a postback since the control cant distinguish which value of "1" the user selected, so it assumed that the first value of "1" it finds is the selected one (he had a list containing 100 values)

    It would have saved that developer (who didnt realise that he's binding a non-unique valued list to the DropDownList) a whole hour of his time if the DropDownList simply threw an item uniqueness exception or something...

Feel free to comment on additional issues you've come across...


some of the control have missing functionality and many of them are heavy when creating light pages (40kb pages). it depends on the need/requirement. for the basic need the will do but many larger companies with develop there own controls that fill there needs.


Awesome, glad to be of help :)

Very good article! Helped me out a lot today!

Post comment

Name *
Body *
Security Code
* Required fields

Related Posts (C#) - WebControlAdapter

2010-03-11 21:47:12

Latest Posts

MS SQL: Parameter Sniffing

2012-05-21 22:38:48

Be the best stalker you can be

2011-12-13 22:33:54

Top 5 posts

Moving items between listboxes in example

Move items between two listboxes in, VB.NET) and PHP
2008-06-12 17:07:43

Simple WYSIWYG Editor

Creating a WYSIWYG textbox for your website is actually quite simple.
2007-02-01 12:00:00

C# YouTube : Google API

Post on how to integrate with YouTube using the Google Data API
2011-03-12 08:37:51

Populate a TreeView Control C#

Populate a TreeView control in a windows application.
2009-08-27 16:01:03

Cross Browser Issues: Firefox Word Wrapping

Firefox word wrapping issues
2008-06-09 09:51:21