ASP.NET MVC ListBox - Client-Side API - Select / Deselect All Items

  • Alfreds FutterkisteALFKI
  • Antonio Moreno TaqueríaANATR
  • Around the HornAROUT
  • Berglunds snabbköpANTON
  • Berglunds snabbköpBERGS
  • Blauer See DelikatessenBLAUS
  • Blondesddsl père et filsBLONP
  • Bólido Comidas preparadasBOLID
  • Bon app'BONAP
  • Bottom-Dollar MarketsBOTTM
Select All Items
Deselect All Items
The ListBox control provides client-side properties / methods for setting / getting the selected item.
By looping through the options collection you can select / deselect all the items from the list.
View - Aspx

    <% Html.Obout(new ListBox("ListBox1")
    {   
        SelectionMode = ListBoxSelectionMode.Multiple
    }); %>
    
    <% Html.Obout(new OboutButton("OboutButton1")
    {
        OnClientClick = "return selectAll();"
    }); %>	   

    <% Html.Obout(new OboutButton("OboutButton2")
    {
        OnClientClick = "return deselectAll();"
    }); %>	   
    
    
View - Razor

    @Html.Obout(new ListBox("ListBox1")
    {   
        SelectionMode = ListBoxSelectionMode.Multiple
    })
    
    @Html.Obout(new OboutButton("OboutButton1")
    {
        OnClientClick = "return selectAll();"
    })

    @Html.Obout(new OboutButton("OboutButton2")
    {
        OnClientClick = "return deselectAll();"
    })
    
    
JavaScript

    function selectAll() {
            for (var i = 0; i < ListBox1.options.length; i++) {
                ListBox1._dropDownList.selectItemByIndex(i, true, false, true, true, true);
            }

            return false;
        }
        function deselectAll() {
            for (var i = 0; i < ListBox1.options.length; i++) {
                ListBox1._dropDownList.unselectItemByIndex(i);
            }

            return false;
        }