Skip to content

Empty Option in DropDownList Bound to LinqDataSource

April 2, 2009

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.

Advertisements
One Comment leave one →
  1. Chris permalink
    May 13, 2009 8:24 am

    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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: