How to deselect item on single select listbox?

Discussion in 'ASP .Net Web Controls' started by rpress, Jan 3, 2006.

  1. rpress

    rpress Guest

    In a multi-select listbox one may deselect items, but this does not
    work in a single select listbox. Has anyone come across a way to do
    this? (deselect so that no items are selected)

    Thanks
    rpress, Jan 3, 2006
    #1
    1. Advertising

  2. 1- Add a HtMLinputButton in the markup
    <input type="button" onclick="ClearList();" value="Clear Selections">

    and add a corresponding javascript:

    <script language="javascript">
    function ClearList()
    {
    var list = document.getElementById("ListBox1");
    if (list) list.selectedIndex =-1;
    }
    </script>

    2- Add a Button server control:
    <asp:Button ID="btnDeselect" Runat="server" Text="De-Select"></asp:Button>

    and in the codebehind:

    Private Sub btnDeselect_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles btnDeselect.Click
    ListBox1.ClearSelection()
    End Sub

    3- You can insert a list item with a blank value for the user to click if
    you wish them to deselect a previously selected item, e.g.

    Dim li As New ListItem("No Selection", "")
    listbox1.Items.Insert(0, li)

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "rpress" wrote:

    > In a multi-select listbox one may deselect items, but this does not
    > work in a single select listbox. Has anyone come across a way to do
    > this? (deselect so that no items are selected)
    >
    > Thanks
    >
    >
    Phillip Williams, Jan 3, 2006
    #2
    1. Advertising

  3. rpress

    rpress Guest

    Thanks for the input - I was hoping for a way to deselect simply by
    clicking on the selected item.

    *** Sent via Developersdex http://www.developersdex.com ***
    rpress, Jan 3, 2006
    #3
  4. Unfortunately the default behavior of the ListBox does not allow for
    deselecting a selected item by clicking on it. You can try handling the
    onclick event to the ListBox (client-side):

    <asp:ListBox ID="ListBox1" Runat="server" onclick="toggle();">
    <%-- here are the list items --%>
    </asp:ListBox>

    and in the Javascript

    function toggle()
    {
    var list = window.event.srcElement;
    if (list)
    {
    if (list.selectedIndex == prevIndex) list.selectedIndex=-1;
    prevIndex = list.selectedIndex;
    }
    }

    This should produce the effect that you were hoping for; namely "to deselect
    simply by clicking on the selected item."

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "rpress" wrote:

    >
    >
    > Thanks for the input - I was hoping for a way to deselect simply by
    > clicking on the selected item.
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    >
    Phillip Williams, Jan 3, 2006
    #4
  5. rpress

    rpress Guest

    rpress, Jan 3, 2006
    #5
  6. rpress

    rpress Guest

    Thanks again - I decided to use the following code on the mousedown
    event to give the effect that a right mouse click clears the listbox.

    function toggle()// toggle clears single select listbox on mouse down
    {
    var list = window.event.srcElement;
    list.selectedIndex=-1;
    }

    works for me. ;>)


    *** Sent via Developersdex http://www.developersdex.com ***
    rpress, Jan 9, 2006
    #6
    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. Bailu
    Replies:
    1
    Views:
    540
  2. Al Knowles

    How to select all or deselect all checkboxes in datagrid...

    Al Knowles, Dec 1, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    224
    Al Knowles
    Dec 1, 2003
  3. Kostia
    Replies:
    2
    Views:
    102
    Eliyahu Goldin
    Jul 4, 2004
  4. Keiron Waites
    Replies:
    1
    Views:
    112
    Keiron Waites
    Oct 21, 2003
  5. Replies:
    10
    Views:
    225
Loading...

Share This Page