Dropdownlist with checkboxes in asp.net

Theo: nguyenhaidang.name.vn | 08/01/2024 - 05:43

 To build this example we will use jquery.sumoselect plugin created by Hemant Negi. This is open source plugin and available on Github. 

This plugin have multiple pre build options like:
  1.  Multiple select
  2. Preselected and disabled
  3. Option to show ok and cancel button
  4. Option to  use placeholder
  5. Option to select all items.

Let''s see how to create dropdownlist with checkboxes in asp .net

 

Step1: Create an asp .net website. 

Step2: Download jquery.sumoselect plugin from here

Step3: Copy jquery.sumoselect.min.js and sumoselect.css into your website folder.

 

Dropdownlist- ith checkboxes in asp .net

 

Step4: Add reference of Jquery, jquery.sumoselect.min.js and sumoselect.css into your website page.

 

   <script src="jquery.sumoselect.min.js"></script>
   <link href="sumoselect.css" rel="stylesheet">

 

Step5: Add Listbox control with some values. I have used static values for this example but you can bind it with database.Also make sure you have set: SelectionMode="Multiple"

 

<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>

 

Step6: Initiate  jquery.sumoselect on document.ready function.

 

<script type="text/javascript">
        $(document).ready(function () {
            $(<%=lstBoxTest.ClientID%>).SumoSelect();
        });
    </script>

 

Dropdownlist with checkboxes in asp .net Output:

Dropdownlist with checkboxes in asp .net


To Get dropdownlist with checkboxes selected values on button click:

 

<asp:button text="Get Values" visible="false" id="btnGetSelectedValues" onclick="btnGetSelectedValues_Click" runat="server"></asp:button>

 

 

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());
   }

 

 

Select all option in Dropdownlist with checkboxes in asp .net.

To enable select all option in dropdownlist with checkboxes you need to change your code like:

 

<script type="text/javascript">
        $(document).ready(function () {
            $(<%=lstBoxTest.ClientID%>).SumoSelect({ selectAll: true });
        });
    </script>

 


Output:

Dropdownlist with checkboxes in asp .net with select all option

Enable Cancel Ok button in Dropdownlist with checkboxes in asp .net

 

To enable cancel and ok button in dropdownlist with checkboxes you need to change your code like:

 

<script type="text/javascript">
        $(document).ready(function () {
              $(<%=lstBoxTest.ClientID%>).SumoSelect({ okCancelInMulti: true });
        });
    </script>

 

 

Output:

Dropdownlist with checkboxes in asp net with ok and cancel button

Back Head Print
Tin khác

Search GridView with Paging on TextBox KeyPress using jQuery in ASP.Net    (28/07/2010)

Bootstrap AutoComplete TextBox example using jQuery TypeAhead plugin in ASP.Net with C# and VB.Net    (28/07/2010)

Disable Button and Submit button after one click using JavaScript and jQuery    (29/07/2010)

Split and convert Comma Separated (Delimited) String to Table in SQL Server    (01/09/2010)

Select Column values as Comma Separated (Delimited) string in SQL Server using COALESCE    (01/09/2010)