clear the display box of dropdownList in code

Discussion in 'ASP .Net' started by Northern, Jul 19, 2003.

  1. Northern

    Northern Guest

    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
    Northern, Jul 19, 2003
    #1
    1. Advertising

  2. 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]

    "Northern" <> wrote in message
    news:002f01c34e23$748ceb00$...
    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
    Ken Cox [Microsoft MVP], Jul 19, 2003
    #2
    1. Advertising

  3. Northern

    Northern Guest

    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
    >-----Original Message-----
    >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]
    >
    >"Northern" <> wrote in message
    >news:002f01c34e23$748ceb00$...
    >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
    >
    >
    >.
    >
    Northern, Jul 19, 2003
    #3
  4. 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]


    "Northern" <> wrote in message
    news:03bb01c34e27$4a275a90$...
    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
    >-----Original Message-----
    >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]
    >
    >"Northern" <> wrote in message
    >news:002f01c34e23$748ceb00$...
    >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
    >
    >
    >.
    >
    Ken Cox [Microsoft MVP], Jul 19, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gnarlodious
    Replies:
    4
    Views:
    17,587
    elizas
    May 5, 2010
  2. Replies:
    9
    Views:
    1,150
  3. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    1,018
    Mark Fitzpatrick
    Jan 31, 2008
  4. Replies:
    0
    Views:
    289
  5. InvalidLastName

    Unrecognized element 'add' after <clear></clear>

    InvalidLastName, Feb 26, 2007, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    956
    Steven Cheng[MSFT]
    Mar 6, 2007
Loading...

Share This Page