Sibusiso Shongwe
Sibusiso Shongwe

Reputation: 175

How to Remove the selected index of a DropDownList?

I have an ASP.Net web for that inserts values into a database, I want to reset the selected items in the dropdownList, how do I do it properly?? I know with textBoxes I can use TextBox1.Text = String.empty but it doesnt seem to work with the dropdownLists

Upvotes: 1

Views: 1974

Answers (4)

Salah Akbari
Salah Akbari

Reputation: 39966

For DropDownList it would be better to first add an empty string at first position of the DropDownList by using yourDropDownList.Items.Insert method and then select it. Something like this:

yourDropDownList.Items.Insert(0, new ListItem(string.Empty, string.Empty));
yourDropDownList.SelectedIndex = 0;

Upvotes: 1

AT-2017
AT-2017

Reputation: 3149

I guess, everyone has given answers. But remember one thing to do as follows:

AppendDataBoundItems="False"

Set it to false as this will cause the rebound data to be appended to the existing list which will not be cleared prior to binding. The above is applicable when data is bound to the DropDownList. The rest will be done clearing it.

Upvotes: 1

Akiner Alkan
Akiner Alkan

Reputation: 6882

Well if you want to clear dropdownList fully so you can clear it's items source such in this way:

ddItems.Clear();

But if you only want to clear selected dropdownListItem than S.Akbari solution:

ddItems.Items.Insert(0, new ListItem(string.Empty, string.Empty));
ddItems.SelectedIndex = 0;

Upvotes: 1

sujith karivelil
sujith karivelil

Reputation: 29026

If you want to clear all Items means you can use

aspDrop.Items.Clear(); // whill clear all the list items

If you want to change the selected index means you have to use

aspDrop.SelectedIndex = someIndex; // where index must be in the list

Or even you can use .Insert() method to insert an item to a particular index and make it as selected item.

Upvotes: 3

Related Questions