Drag and drop in ListBox Control

3 Mar 20233 minutes to read

The ListBox has support to drag an item or a group of selected items and drop it within the same list box or into another list box.

The elements can be customized on drag and drop by using the following events,

Events Description
dragStart Triggers when the selected element is being dragged.
drag Triggers when the selected element is being dragged.
drop Triggers when the selected element is being dropped.

Single listbox

To drag and drop an item or group of item within the list box can be achieved by setting allowDragAndDrop property as true.

The following sample illustrates how to drag and drop an item within the same list box by enabling allowDragAndDrop property.

<ejs-listbox id="listbox" dataSource="@ViewBag.data" allowDragAndDrop="true">
</ejs-listbox>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class ListBoxController : Controller
    {
        public IActionResult dragdrop()
        {
            ViewBag.data = new string[] { "Austrlia", "Bermuda", "Canada", "Cameroon", "Denmark", "France", "Finland", "Germany", "Hong kong" };
            return View();
        }
    }
}

Multiple listbox

To drag and drop an item or group of item between two list boxes can be achieved by setting allowDragAndDrop property as true and scope property should be set to both the list boxes.

In the following sample, the allowDragAndDrop property is set as true and scope is set as combined-list to enable drop and drop in both list boxes.

<div style="width:50%; margin:auto">
    <div style="float:left; width:48%">
        <ejs-listbox id="listbox1" dataSource="@ViewBag.groupA" allowDragAndDrop="true" scope="combined-list" height="290px"></ejs-listbox>
    </div>
    <div style="float:right; width:48%">
        <ejs-listbox id="listbox2" dataSource="@ViewBag.groupB" allowDragAndDrop="true" scope="combined-list" height="290px"></ejs-listbox>
    </div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class ListBoxController : Controller
    {
        public IActionResult dragdrop()
        {
            ViewBag.groupA = new string[] { "Austrlia", "Bermuda", "Canada", "Cameroon", "Denmark", "France", "Finland", "Germany", "Hong kong" };
            ViewBag.groupB = new string[] { "India", "Italy", "Japan", "Mexico", "Norway", "Poland", "Switzerland", "United Kingdom", "United States" };
            return View();
        }
    }
}