clear the display box of dropdownList in code

  • Thread starter Thread starter Northern
  • Start date Start date
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
 
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
 
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
 
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
 
Back
Top