演示:
原文地址:http://www.cnblogs.com/puke/articles/558123.html
感谢楼主强大的JS功底...
JS 代码
<!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 id="Head1" runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function btn_right_onclick()
{
var select_ListBox_left = document.getElementById("ListBox_left");
var select_ListBox_right = document.getElementById("ListBox_right");
var count = 0;
for(i = 0; i < select_ListBox_left.length; i ++)
{ count = 0;
if(select_ListBox_left.options[i].selected)
{
var temp = document.createElement("option");
temp.value = select_ListBox_left[i].value;
temp.text = select_ListBox_left[i].text;
if(select_ListBox_right.length == 0)
{
select_ListBox_right.add(temp);
}
else
{
for(j = 0; j < select_ListBox_right.length; j ++)
{
if(select_ListBox_right.options[j].value == temp.value)
{
count++;
}
}
if(count == 0)
{
select_ListBox_right.add(temp);
}
}
}
}
}
function btn_QuanBu_right_onclick()
{
var select_ListBox_left = document.getElementById("ListBox_left");
var select_ListBox_right = document.getElementById("ListBox_right");
var tmp = select_ListBox_right.length;
var rightoldcount= <%=oldcount%>;
if(tmp>rightoldcount)
{
for(i = tmp; i >= rightoldcount; i --)
{
select_ListBox_right.remove([i]);
}
}
for(j = 0; j < select_ListBox_left.length; j++)
{
var temp = document.createElement("option");
temp.value = select_ListBox_left[j].value;
temp.text = select_ListBox_left[j].text;
select_ListBox_right.add(temp);
}
}
function btn_left_onclick()
{
var select_ListBox_right = document.getElementById("ListBox_right");
var tmp = select_ListBox_right.length;
var rightoldcount= <%=oldcount%>;
if(tmp>rightoldcount)
{
for(i = rightoldcount; i < tmp; i ++)
{
if(select_ListBox_right.options[i].selected)
{
select_ListBox_right.remove([i]);
tmp--;
}
}
}
}
function btn_QuanBu_left_onclick()
{
var select_ListBox_left = document.getElementById("ListBox_left");
var select_ListBox_right = document.getElementById("ListBox_right");
var tmp = select_ListBox_right.length;
var rightoldcount= <%=oldcount%>;
if(tmp>rightoldcount)
{
for(i = tmp; i >= rightoldcount; i --)
{
select_ListBox_right.remove([i]);
}
}
}
function xzall()
{
var select_ListBox_right = document.getElementById("ListBox_right");
var tmp = select_ListBox_right.length;
var rightoldcount= <%=oldcount%>;
var s="";
if(tmp>rightoldcount)
{
for(i = rightoldcount; i < tmp; i ++)
{
s+=select_ListBox_right.options[i].value+"|";
}
}
alert(s);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="250px" border="0" cellpadding="0" cellspacing="0">
<tr><td>需要调度的部门</td><td></td><td>新部门已有成员</td></tr>
<tr>
<td><asp:ListBox ID="ListBox_left" SelectionMode="Multiple" Height="200px" Width="230px" runat="server"></asp:ListBox></td>
<td>
<input id="btn_right" type="button" value=" > " οnclick="return btn_right_onclick()" />
<input id="btn_QuanBu_right" type="button" value=" >> " οnclick="return btn_QuanBu_right_onclick()" />
<input id="btn_left" type="button" value=" < " οnclick="return btn_left_onclick()" />
<input id="btn_QuanBu_left" type="button" value=" << " οnclick="return btn_QuanBu_left_onclick()" />
</td>
<td style="width: 111px"><asp:ListBox ID="ListBox_right" SelectionMode="Multiple" Height="200px" Width="237px" runat="server"></asp:ListBox></td>
</tr>
</table>
</div>
<input id="btnselect" type="button" value="选中" οnclick="xzall()" />
</form>
</body>
</html>
后台代码:
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.ApplicationBlocks.Data;
public partial class _Default : System.Web.UI.Page
{
protected int oldcount;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
Bind1();
Bind2();
}
}
private void Bind1() {
string conn=ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
string sql = "select top 10 orderid,shipaddress from orders";
DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
this.ListBox_left.DataSource = ds;
this.ListBox_left.DataTextField = "shipaddress";
this.ListBox_left.DataValueField = "orderid";
this.ListBox_left.DataBind();
}
private void Bind2()
{
string conn = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
string sql = "select top 5 orderid,shipaddress from orders order by orderid desc";
DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
oldcount = ds.Tables[0].Rows.Count;
this.ListBox_right.DataSource = ds;
this.ListBox_right.DataTextField = "shipaddress";
this.ListBox_right.DataValueField = "orderid";
this.ListBox_right.DataBind();
}
}