views:

53

answers:

2

I have a DataBound DropDownList that is loading fine, but i'd like to insert another item that says "--Select--" or something instead of having the first DataBound item automatically display.

Is there an easy way to do this or do I need to manually add a dummy item?

Here is my code:

            MyContext fc = new MyContext ();
            ddl.DataSource = fc.SomeTable;
            ddl.DataBind();
+1  A: 

After you do the databind do:

ddl.Items.Insert(0, "---Select---");

This will add it as the first item in the list.

Alternatively, you can add a new ListItem instead of a string, so you can have an actual value instead of a string as drop down list value.

So you can do something like:

ddl.Items.Insert(0, new ListItem("---Select---", Guid.Empty.ToString());
Kevin
+3  A: 

Alternatively, add a default item in the markup and set the "AppendDataBoundItems" property to true.

     <asp:DropDownList ID="ddl" runat="server" AppendDataBoundItems="true">

       <asp:ListItem Value="" Text="---Please Select---"></asp:ListItem>

   </asp:DropDownList>
Damien Dennehy
This will be gone after the DataBind would it not?
Abe Miessler
@Abe Miessler: No, because AppendDataBoundItems="true"
Claudio Redi