sarsnake
sarsnake

Reputation: 27703

Adding a blank selection item to the list in drop down list

my dropdownlist is populated from the database as in:

DataTable dt = GetData(); ddlMylist.DataSource = dt; ddlMylist.DataBind();

Now dt contains the data, and I wish to add "Select" word to the top of the list when the selection is blank. It seems as there is no option other than add it to the dt (DataTable object)....but it seems wrong somehow.

What are other options. It's doesn't have to be the word "Select" it can be just an empty space...Currently, upon page load I can see the first data item in the list which is all good and dandy, but I have 3 drop downs that are co-dependent, and I want the user to explicitly make a selection which will trigger other drop downs to populate accordingly.

Upvotes: 7

Views: 38725

Answers (4)

COMPILER
COMPILER

Reputation: 23

Try this one:

ddlMylist.Items.Insert(0, "Select");

Upvotes: 2

Phil Sandler
Phil Sandler

Reputation: 28016

Try:

ddlMylist.Items.Insert(0, new ListItem([key], [text]));
ddlMylist.SelectedIndex = 0;

You do this after you databind to your source.

Upvotes: 13

Joel Coehoorn
Joel Coehoorn

Reputation: 415665

Your dropdownlist markup should look like this:

<asp:DropDownList ID="ddlMylist" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Text="-Select-" Value="" />
</asp:DropDownList>

Note the AppendDataBoundItems attribute.

Upvotes: 8

Cory Charlton
Cory Charlton

Reputation: 8938

Try:

DDL.Text = string.Empty;

Edit:

I know this works when I'm manually adding items but I'm not sure if it will when a DataSource is bound.

Upvotes: 0

Related Questions