Multiselect Dropdownlist With Checkbox in ASP.NET C#

Codingvila
0
In this article, I will explain how to implement a jquery multi-select drop-down list with a checkbox with Bootstrap in asp.net with c# and vb.net and show you how to display selected items/checked values indicate 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 working with a web application with bootstrap 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 a 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 Items is greater than 3 it should be displayed count of selected items in the drop-down.
6) Display an alert box with selected item text and its selected values at 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 the stylesheet and Javascript library of Bootstrap. here we will include these libraries using a CDN before the 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 the end <body> tag.
<script type="text/javascript">
    $(function () {
        $('[id*=lstStudents]').multiselect
        ({
            includeSelectAllOption: true
        });
    });
</script>
If you analyzed the 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 below.
<script type="text/javascript">
    $(function () {
        $('[id*=lstStudents]').multiselect
        ({
            includeSelectAllOption: false
        });
    });
</script>
If you want to show the default value on aspx control <asp: ListBox>(DropDownList control) so that the user can understand what 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 its selected values at the click of a button. and for that, you need to add an 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 looks 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 the 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 for each loop to get selected items from drop-down control also declared a string variable with a name message, and simply added/concrete the name of the student and his/her enrollment no with string variable message. and finally, as per our requirement, this message is 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 a 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 their value based on checkbox selection.

You can contact us at info.codingvila@gmail.com if you want source code or need any live support.

Price for Source Code: $10

Price for Live Support: $12 / hour

Post a Comment

0 Comments

Thank you for your valuable time, to read this article, If you like this article, please share this article and post your valuable comments.

Once, you post your comment, we will review your posted comment and publish it. It may take a time around 24 business working hours.

Sometimes I not able to give detailed level explanation for your questions or comments, if you want detailed explanation, your can mansion your contact email id along with your question or you can do select given checkbox "Notify me" the time of write comment. So we can drop mail to you.

If you have any questions regarding this article/blog you can contact us on info.codingvila@gmail.com

Post a Comment
To Top