Drag n Drop Inside 2 Listbox in Asp.Net
I want to implement Drag n Drop Functionality in 2 List/Combo Box in Asp.Net.I will sel开发者_如何学运维ect and drag 1 item from listbox1 and drop it on Listbox2. So How should i start with ?
Is there any Jquery Plugin Available ?
Thank
Rick Jackson
There are a lot of plugins available on net,but you can achieved the by using java script.I have created a sample app in asp.net.Check out this and let me know if you have any doubt.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ST.aspx.cs" Inherits="ST" %>
<!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></title>
<script language="javascript" type="text/javascript">
var d;
function drag(objSource) {
this.select = objSource;
}
function drag.prototype.drop(objDest) {
if (!this.dragStart) return;
this.dest = objDest;
var o = this.option.cloneNode(true);
this.dest.appendChild(o);
this.select.removeChild(this.option);
}
function drag.prototype.setIndex() {
var i = this.select.selectedIndex;
//i returns -1 if no option is "truly" selected
window.status = "selectedIndex = " + i;
if (i == -1) return;
this.option = this.select.options[i];
this.dragStart = true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="one" runat="server" onmousedown="d = new drag(this);" onmouseup="d.drop(this.form.two);"
onmouseout="if (typeof d != 'undefined') d.setIndex();">
<asp:ListItem Text="Opt1" Value="1"></asp:ListItem>
<asp:ListItem Text="Opt2" Value="2"></asp:ListItem>
<asp:ListItem Text="Opt3" Value="3"></asp:ListItem>
</asp:ListBox>
<asp:ListBox ID="two" runat="server">
<asp:ListItem Text="Opt1" Value="1"></asp:ListItem>
<asp:ListItem Text="Opt2" Value="2"></asp:ListItem>
<asp:ListItem Text="Opt3" Value="3"></asp:ListItem>
</asp:ListBox>
</div>
</form>
</body>
</html>
I'm using this two great plugins for drag and drop. They have a lot of ready to use samples:
http://threedubmedia.com/code/event/drag
http://threedubmedia.com/code/event/drop
This is how I achieved it. The data comes from the database, and I am using a listview to display the list.
<table>
<tr>
<td>
<asp:ListView ID="Listview1" runat="server" DataSourceID="LinqDataSource1" >
<LayoutTemplate>
Unallocated Sections
<asp:Label ID="Setter Count" runat="server"></asp:Label>
<ul class="sortable" draggable="true" id="Setter" style="overflow: scroll; empty-cells: hide; height: 500px; width: 200px; border: solid 1px; background: #b6b6b6;" >
<li id="Li1" runat="server" draggable="true" style="user-select: element;" >
<asp:PlaceHolder ID="PlaceHolder1" id-="itemPlaceholder" runat="server" ></asp:PlaceHolder>
</li>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li id="Li2" runat="server" title="Drag to Allocate sections" >
<%# Eval("Section")%>
</li>
</ItemTemplate>
</asp:ListView>
</td>
<td>Allocated Sections
<asp:Label ID="GetterCount" runat ="server"></asp:Label>
<ul class="sortable" draggable="true" id="Getter" style="overflow: scroll; height: 500px; width: 200px; border: solid 1px; background: #fff;">
<li style="color: red;"></li>
</ul>
</td>
</tr>
</table>
The jQuery used is as follows:
<script>
$(function () {
$("#Setter,#Getter").sortable({
connectWith: ".sortable"
}).selectable();
});
</script>
I hope this helps. For more options you can check jQuery UI: Sortable.
精彩评论