In this article I will explain how to implement a Multiple Select (MultiSelect) DropDownList with CheckBoxes in ASP.Net with jQuery Bootstrap Multi-Select Plugin.
So as to actualize a Multiple Select (MultiSelect) DropDownList with CheckBoxes in ASP.Net we should make use of ListBox control and apply the jQuery Bootstrap Multi-Select Plugin to it.
You can follow these simple steps to implement a Multiple Select DropdownList with CheckBox.
I am creating a MVC application and create controller "Home"
In next step create Action to open view "Index" see example.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace DropdownCheckbox.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- }
- }
- @{
- ViewBag.Title = "Home Page";
- Layout = null;
- }
- <div>
- <table>
- <tr>
- <td><p >Select Expert's Name </p></td>
- <td>
- <select id="EmpList" multiple="multiple">
- <option value="1">Navdeep-Asp.net</option>
- <option value="2">Pankaj-C#</option>
- <option value="3">Bikesh-Asp.Net</option>
- <option value="4">Pritam-Salesforce</option>
- <option value="5">Payal-Salesforce</option>
- <option value="6">Sujata-SSRS</option>
- <option value="7">Harikant-UI</option>
- </select>
- </td>
- <td><input type="button" id="btnSelected" value="Get Selected" /></td>
- </tr>
- </table>
- </div>
- <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>
After that write js code to implement multiselect with checkbox in dropdown.use this code.
- <script type="text/javascript">
- $(function () {
- $('#EmpList').multiselect({
- includeSelectAllOption: true
- });
- $('#btnSelected').click(function () {
- var selected = $("#EmpList option:selected");
- var message = "";
- selected.each(function () {
- message += $(this).text() + " " + $(this).val() + "\n";
- });
- alert(message);
- });
- });
- </script>
Set routing inside "route.config" file.see code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- namespace DropdownCheckbox
- {
- public class RouteConfig
- {
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
- }
- }
- }
You can see and understand complete code.
- @{
- ViewBag.Title = "Home Page";
- Layout = null;
- }
- <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>
- <script type="text/javascript">
- $(function () {
- $('#EmpList').multiselect({
- includeSelectAllOption: true
- });
- $('#btnSelected').click(function () {
- var selected = $("#EmpList option:selected");
- var message = "";
- selected.each(function () {
- message += $(this).text() + " " + $(this).val() + "\n";
- });
- alert(message);
- });
- });
- </script>
- <div>
- <table>
- <tr>
- <td><p >Select Expert's Name </p></td>
- <td>
- <select id="EmpList" multiple="multiple">
- <option value="1">Navdeep-Asp.net</option>
- <option value="2">Pankaj-C#</option>
- <option value="3">Bikesh-Asp.Net</option>
- <option value="4">Pritam-Salesforce</option>
- <option value="5">Payal-Salesforce</option>
- <option value="6">Sujata-SSRS</option>
- <option value="7">Harikant-UI</option>
- </select>
- </td>
- <td><input type="button" id="btnSelected" value="Get Selected" /></td>
- </tr>
- </table>
- </div>
According this example i can select any one or multiple,also can see count of selected items.
We can see item on popup after click on "GetSelected".
I hope you enjoyed with this blog and learned lot of thinks. If you have any confusion regarding this topics you can download project.
You have just read an article that categorized by title MVC
by title Dropdown with Multiple checkbox select with jQuery in MVC 5. You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/09/part-38dropdown-with-multiple-checkbox.html. Thank You!
Author:
Bikesh Srivastava - Thursday, September 29, 2016
The plugins used here is creating conflicts and is not allowing to load jqgrid
ReplyDeleteThe plugins used here is creating conflicts and is not allowing to load jqgrid
ReplyDeleteIf you are facing conflict problem in j query use given code to resolve
DeletejQuery.noConflict();
Code works .useful for beginners. Keep updating UI online course
ReplyDeleteGood Day, I am using the code as is and it shows my control as a listbox, am i mussing something
ReplyDeleteMay be jquery not working .share screenshot
DeleteReally Good blog post.provided a helpful information.I hope that you will post more updates like this AngularJS Online Training
ReplyDeleteCheck box not coming inside dropdownlist, plz suggest
ReplyDelete