clear the display box of dropdownList in code

N

Northern

I need to write code clear the display box of my
DropDownList (something like clear current selected item)
while still keep the loaded item list in the DropDownList.

I tried DropDownList's ClearSelecttion() method and set
SelectedIndex to -1 but neither works.

How should I do this?

Thanks
 
K

Ken Cox [Microsoft MVP]

Is it that you just want to return to the default setting?

DropDownList1.SelectedIndex = 0

Or remove the one that is selected and leave the rest?

DropDownList1.Items.RemoveAt(DropDownList1.SelectedIndex)

Ken
MVP [ASP.NET]

I need to write code clear the display box of my
DropDownList (something like clear current selected item)
while still keep the loaded item list in the DropDownList.

I tried DropDownList's ClearSelecttion() method and set
SelectedIndex to -1 but neither works.

How should I do this?

Thanks
 
N

Northern

I guess I didn't phrase my question clearly.
I don't want to remove anything from the list's item
collection. What I want is just to clear the current
selection so that the display box of the dropdownbox on
the form will be blank. But if now I click on the dropdown
again, I can see all items are still there.

Thanks
 
K

Ken Cox [Microsoft MVP]

I think the dropdownlist defaults to the first item in the order.

If you want nothing to appear, you would have to have a "blank" item as the
first item in the list. You could either add this when you build the
dropdownlist or later when you wanted to reset the appearance:

Private Sub Button2_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button2.Click
If DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("")) = -1
Then
DropDownList1.Items.Insert(0, "")
End If
DropDownList1.SelectedIndex = 0
End Sub

Ken
MVP [ASP.NET]


I guess I didn't phrase my question clearly.
I don't want to remove anything from the list's item
collection. What I want is just to clear the current
selection so that the display box of the dropdownbox on
the form will be blank. But if now I click on the dropdown
again, I can see all items are still there.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top