views:

203

answers:

4

I have a ASP DropDownList with item added to it. all what I want is to make the selection after the page loaded empty so there should not be selected item.

How can I do that.

+1  A: 

You can set the SelectedIndex property to -1 or you can add an empty entry as the first item in the data source and validate the selection on form submission.

NickLarsen
"You can set the SelectedIndex property to -1"this one did not work I set SelectedIndex property to -1 in loadpage event but did not work and it was working if set it to 2 or 3 but not -1.
Eyla
A: 

yourDropDownList.Items.Clear()

To repopulate, you can either add items statically as per womps suggestion (substituting params in the insert() method, or you can populate it dynamically from a data source. The backing store for list items is the ListItemCollection.

flesh
This will remove all items, not just clear the selected item.
womp
i agree, the question is ambiguous though
flesh
Agree on the ambiguity - I was just helping to clear up what it would do. Not sure why you were downvoted - sorry if my comment cause that :(
womp
this one will clear the item list and in this case I need another method to load the list again.
Eyla
+2  A: 

You can add an empty item to the top of your dropdownlist programmatically like this:

myDropDown.Items.Insert(0, new ListItem(string.Empty, string.Empty));
myDropDown.SelectedIndex = 0;
womp
Thank you this one worked well!!!
Eyla
A: 

Not sure I understand your question but try this:

DropDownList1.ClearSelection()

or

DropDownList1.SelectedIndex = -1;
Naeem Sarfraz