Click here to Skip to main content
15,887,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Sumoselect jquery for populating the dropdown list in my ASP.Net WebForms page, but on page load I am unable to make the selection "Select All" checked so that all the dropdowns values were being selected on page load.

What I have tried:

This is my aspx code---

<%@ Page Language="C#" AutoEventWireup="true" Inherits="Dropdown1" CodeBehind="Dropdown1.aspx.cs" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script type="text/javascript" src="jquery.sumoselect.min.js"></script>
    <link href="sumoselect.css" rel="stylesheet" />

    <script type="text/javascript">

        $(document).ready(function () {
            $('#<%=lstBoxTest.ClientID%>').SumoSelect({selectAll:true});
        });


    </script>
    <style type="text/css">
        body {
            font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
            color: #444;
            font-size: 13px;
        }

        p, div, ul, li {
            padding: 0px;
            margin: 0px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ListBox runat="server" ID="lstBoxTest" SelectionMode="Multiple">
            <asp:ListItem Text="Red" Value="0"></asp:ListItem>
            <asp:ListItem Text="Green" Value="1"></asp:ListItem>
            <asp:ListItem Text="Yellow" Value="2"></asp:ListItem>
            <asp:ListItem Text="Blue" Value="3"></asp:ListItem>
            <asp:ListItem Text="Black" Value="4"></asp:ListItem>
        </asp:ListBox>
        <asp:Button Text="Get Values" Visible="false" ID="btnGetSelectedValues" OnClick="btnGetSelectedValues_Click" runat="server" />
    </form>
</body>
</html>





and this is my code behind---

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


    public partial class Dropdown1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


        }


        protected void btnGetSelectedValues_Click(object sender, EventArgs e)
        {
            string selectedValues = string.Empty;



            foreach (ListItem li in lstBoxTest.Items)
            {
                if (li.Selected == true)
                {
                    selectedValues += li.Text + ",";
                }
            }

            
            Response.Write(selectedValues.ToString());
        }



    }
Posted
Updated 1-Nov-18 9:17am

Looking at the documentation[^], you need to call the selectAll method:
$('#<%=lstBoxTest.ClientID%>').SumoSelect({selectAll:true})[0].sumo.selectAll();

However, this will overwrite the user's selection every time they press the button. You'll want to make sure you only call the selectAll method on the first load, or when no items are selected. For example:
JavaScript
$(function(){
    var list = $('#<%=lstBoxTest.ClientID%>');
    list.SumoSelect({selectAll:true});
    
    if (!list.find("option:selected").length) {
        // No items are selected:
        list[0].sumo.selectAll();
    }
});
 
Share this answer
 
Comments
Member 13440616 20-Dec-17 5:34am    
No this is also not working
Richard Deeming 20-Dec-17 8:01am    
Then you'll need to debug your code to find out why. Or report the bug to the library's authors.
 <script>  
       function selectDeselect() {
          
           var listb = document.getElementById('<%=lstBoxTest.ClientID %>');  
          
            var len = listb.options.length;  
            for (var i = 0; i < len; i++) {  
                
                listb.options[i].selected = true;  
            }  
        }  
        
      </script>  
    <style type="text/css">
        body {
            font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
            color: #444;
            font-size: 13px;
        }
 
        p, div, ul, li {
            padding: 0px;
            margin: 0px;
        }
    </style>
</head>
<body onload="selectDeselect();">
    <div style="margin-left:200px;">
    <form id="form1" runat="server">
        <asp:ListBox runat="server" ID="lstBoxTest" SelectionMode="Multiple" Width="300px">
            <asp:ListItem Text="Red" Value="0"></asp:ListItem>
            <asp:ListItem Text="Green" Value="1"></asp:ListItem>
            <asp:ListItem Text="Yellow" Value="2"></asp:ListItem>
            <asp:ListItem Text="Blue" Value="3"></asp:ListItem>
            <asp:ListItem Text="Black" Value="4"></asp:ListItem>
        </asp:ListBox>
        <asp:Button Text="Get Values" Visible="false" ID="btnGetSelectedValues" OnClick="btnGetSelectedValues_Click" runat="server" />
    </form>
        </div>
</body>
 
Share this answer
 
Comments
Richard Deeming 18-Dec-17 13:47pm    
I'm guessing you didn't even try that solution? Since you're overwriting the user's selection every time they click the button...
Member 13440616 20-Dec-17 5:31am    
Its not working as I expected
$( document ).ready(function() {

var list = $('#<%=cyclst.ClientID%>');
list.SumoSelect({selectAll:true});

if (!list.find("option:selected").length) {
// No items are selected:
list[0].sumo.selectAll();
}
});

This is working for me.
 
Share this answer
 
v2
Comments
CHill60 2-Nov-18 9:17am    
This is Solution 2 reposted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900