Empty Option in DropDownList Bound to LinqDataSource

2009 April 2
by echostorm

This has been driving me a little crazy.  Sure if it was a completely code side binding I could insert the empty row into the datasource myself but I’m doing lots of work in ListViews of late so that option is totally out.  I ended up finding the answer on StackOverflow.

<asp:DropDownList ID="ddlEditEmployee" AppendDataBoundItems="true"

runat="server"

DataTextField="Value" DataValueField="ID"

SelectedValue='<%# Bind("EmployeeID") %>'

DataSourceID="LinqDataSource2">

<asp:ListItem Value="-1">

None

</asp:ListItem>

</asp:DropDownList>

The AppendDataBoundItems bit is important too otherwise sneaking that default on in there is for nothing. Enjoy.

One Response leave one →
  1. 2009 May 13
    Chris permalink

    When I found your site I thought… “Why didn’t I know Adam had a blog?”
    Then I looked over your blog postings and remembered… because every time I find your blog my eyes go glassy from all the code and I black out. This is by far the furthest I’ve made it into your blog.

    hey… I sent you an email to an old address I had for you, if it doesn’t reach you get me back at the email address I put here.

    -Chris

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS