Bootstrap JQuery Multiselect Dropdown List With Checkbox in ASP.NET Using C# And VB.NET

watch_later Monday, January 7, 2019
comment 2 Comments

Introduction


In this article, I am going to explain how to implement jquery multi-select drop-down list with a checkbox with bootstrap in asp.net with c# and vb.net and also show you how you can display selected item/checked values display in the drop-down list.
jquery multi-select drop-down list with a checkbox
jquery multi-select drop-down list with a checkbox

What is jQuery Bootstrap Multi-Select Plugin?


JQuery bootstrap multi-select drop-down list plugin allows the users/visitor to select multiple options from a drop-down select list containing the single options as checkboxes.

While you working with a web application with bootstrap some time within your form you need a control like a drop-down list with multiple checkboxes where you need to select multiple items based on checkbox selection, such as party name, date, list of fruits and etc based on your requirement.

Requirement


1 ) Create bootstrap muli-select drop-down list with the checkbox selection.
2 ) Allows the users/visitor to select multiple options from a drop-down list.
3 ) The select list containing the single options as checkboxes.
4 ) Selected Item should be displayed in the drop-down as comma separated string.
5 ) If the count of Selected Item is greater then 3 then it should be displayed count of selected items in the drop-down.
6) Display an alert box with selected item text and it's selected values on the click of the button.

Implementation


First, we need to include the jQuery library with our web page to use the multi-select plugin and also need to include stylesheet and Javascript library of bootstrap. here we will include these libraries using a CDN before end <head> tag.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css" rel="stylesheet" type="text/css" />
<script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js" type="text/javascript"></script>
Note: For using CDN you may have to adjust the version of CDN.

Now, we will simply use aspx control <asp:ListBox> to create our select input which you want to turn into a multi-select and remember that the multiple attributes as to get a real multi-select. Now, we just have to add list items to aspx control <asp:ListBox>.
<asp:ListBox ID="lstStudents" CssClass="dropdown dropdown-menu" runat="server" SelectionMode="Multiple">
    <asp:ListItem Text="Nikunj Satasiya" Value="1" />
    <asp:ListItem Text="Ronak Rabadiya" Value="2" />
    <asp:ListItem Text="Hiren Dobariya" Value="3" />
    <asp:ListItem Text="Vivek Ghadiya" Value="4" />
    <asp:ListItem Text="Pratik Pansuriya" Value="5" />
    <asp:ListItem Text="Kishan Patel" Value="6" />
</asp:ListBox>
Now, simply I applying the jQuery bootstrap multi-select plugin to the aspx control <asp:ListBox> DropDownList control. and for that, we will write the following script before end <body> tag.
<script type="text/javascript">
    $(function () {
        $('[id*=lstStudents]').multiselect
        ({
            includeSelectAllOption: true
        });
    });
</script>
If you analyzed above script then there is Select all option is included and you can remove that Select all option as per your requirements/needs by setting includeSelectAllOption to false or by removing includeSelectAllOption as shown as below.
<script type="text/javascript">
    $(function () {
        $('[id*=lstStudents]').multiselect
        ({
            includeSelectAllOption: false
        });
    });
</script>
If you want to show default value on aspx control <asp:ListBox>(DropDownList control) so that user can understand this drop-down is for what. E.g, Select Students, Select Employees, Select Country, Select State and etc, then you can modify this script as follows.
<script type="text/javascript">
    $(function () {
        $('[id*=lstStudents]').multiselect
        ({
            includeSelectAllOption: true,
            nonSelectedText: 'Select Students' // Here you can change with your desired text as per your requirement.
        });
    });
</script>
Now, as per our requirement, we will display an alert box with selected item text and it's selected values on the click of the button. and for that, you need to add aspx button control within your web page.
<asp:Button Text="Submit Students" CssClass="btn btn-default btn-success" runat="server" OnClick="Submit" />
Now you can see our full HTML Markup look like something :

HTML Markup

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>MultiSelect DropDownList with CheckBoxes in ASP.Net</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
    <link href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css" rel="stylesheet" type="text/css" />
    <script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <h1 class="h2">Bootstrap Muli-Select Drop-Down List With The Checkbox Using JQuery</h1>
            <br>
            <asp:ListBox ID="lstStudents" CssClass="dropdown dropdown-menu" runat="server" SelectionMode="Multiple">
                <asp:ListItem Text="Nikunj Satasiya" Value="1" />
                <asp:ListItem Text="Ronak Rabadiya" Value="2" />
                <asp:ListItem Text="Hiren Dobariya" Value="3" />
                <asp:ListItem Text="Vivek Ghadiya" Value="4" />
                <asp:ListItem Text="Pratik Pansuriya" Value="5" />
                <asp:ListItem Text="Kishan Patel" Value="6" />
            </asp:ListBox>
            <div>
                <br>
                <asp:Button Text="Submit Students" CssClass="btn btn-default btn-success" runat="server" OnClick="Submit" />
            </div>
            <br>
        </div>
    </form>
    <script type="text/javascript">
        $(function () {
            $('[id*=lstStudents]').multiselect
            ({
                includeSelectAllOption: true,
                nonSelectedText: 'Select Students' // Here you can change with your desired text as per your requirement.
            });
        });
    </script>
</body>
</html>
Now, to archive our requirement we will write following code in c# and vb.net.

C#

public partial class CS : System.Web.UI.Page
{
    protected void Submit(object sender, EventArgs e)
    {
        string message = "";
        foreach (ListItem item in lstStudents.Items)
        {
            if (item.Selected)
            {
                message += "Student Name : " + item.Text + ", Enrollment No : " + item.Value + "\\n";
            }
        }
        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert""alert('" + message + "');"true);
    }
}

VB.NET

Partial Class VB
    Inherits System.Web.UI.Page
    Protected Sub Submit(sender As Object, e As EventArgs)
        Dim message As String = ""
        For Each item As ListItem In lstStudents.Items
            If item.Selected Then
                message += "Student Name : " + item.Text + ", Enrollment No : " + item.Value + "\n"
            End If
        Next
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert""alert('" & message & "');"True)
    End Sub
End Class

Explanation

If you analyzed the above code then I have used the foreach loop to get selected items from drop-down control and also declared string variable with name message, and simply add/concate the name of students and his/her enrollment no with string variable message. and finally as per our requirement this message displayed in the alert box.

Output

Bootstrap JQuery Multiselect Dropdown List With Checkbox
Bootstrap JQuery Multiselect Dropdown List With Checkbox

Summary/Conclusion


This article explains how you can easily implement jquery multi-select drop-down list with a checkbox with bootstrap in asp.net with c# and vb.net and also can get selected item names and its value based on checkbox selection.

Codingvila provides articles and blogs on web and software development for beginners as well as free Academic projects for final year students in Asp.Net, MVC, C#, Vb.Net, SQL Server, Angular Js, Android, PHP, Java, Python, Desktop Software Application and etc.

avatar

HI Team,
Object doesn't support property or method 'multiselect'
I am getting this error. Could you tell me the reason?WHy I'm getting this!

delete May 13, 2020 at 8:44 PM
avatar
Codingvila person

Hi, I think issue is your call of your script, you can add your script in head tag and try, it should be work. Still if you get same issue then let us know we will help you. You can drop mail to us on info.codingvila@ gmail.com

delete May 14, 2020 at 3:09 PM



sentiment_satisfied Emoticon