Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you remove items from an ASP.NET Drop Down List without knowing anything about contents

Tags:

asp.net

I have a drop down list in ASP.NET.

I want to remove every item from this list apart from the first one.

I see there is a Remove methond on items but it dosn't seeem to meet my needs.

Can this be done?

like image 845
AJM Avatar asked Jul 27 '09 12:07

AJM


2 Answers

Here is the code for the clear method:

var listItem = DropDownList.Items[0];

DropDownList.Items.Clear();


DropDownList.Items.Add(listItem);
like image 143
kemiller2002 Avatar answered Oct 14 '22 03:10

kemiller2002


you could grab the first element and store it in a local variable, then remove all items (Clear method) and finally add the first element again.

like image 29
Stefan Egli Avatar answered Oct 14 '22 03:10

Stefan Egli