Kevin
Kevin

Reputation: 4848

Change blank selection in drop down list?

I have a databound dropdown list on my page. The first value in the selection list is blank. Is there a way that I can change it so that the first selection says "Unassigned" instead of blank? I've tried the following, but it didn't work:

        // Insert 'Unassigned' value for artist dropdown
        ddlArtists.Items.Insert(0, "Unassigned");

After inserting the above code, the list still appears unchanged, with the first selection value being a blank. Any pointers would be great! Thank you!

EDIT: Here is the code for the dropdown:

<asp:DropDownList ID="ddlArtists" runat="server" Width="130px" TabIndex="5"
 OnSelectedIndexChanged="ddlArtists_SelectedIndexChanged"
 DataSourceID="sqldsArtist" DataTextField="Name" DataValueField="ID" 
 OnDataBound="ddl_DataBound"
 AutoPostBack="True">

Upvotes: 0

Views: 152

Answers (2)

emerson.marini
emerson.marini

Reputation: 9348

You don't need to do it on the CodeBehind. Just do it like that:

<asp:DropDownList ID="ddlArtists" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Text="Unassigned" Value="0" Selected="true" />
</asp:DropDownList>

The AppendDataBoundItems property defines whether the contents of the DropDownList should be cleared out before data binding. Don't forget to check for a PostBack when you're data binding it, to avoid duplicates.

Upvotes: 3

FreddieH
FreddieH

Reputation: 813

Set the SelectedIndex property of your DropDownList to 0.

Upvotes: 1

Related Questions