ASP.NET ListBox控件左移右移

演示:

9f27b3a72109b283d1435831.jpg

原文地址: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()" />
                    &nbsp;
                </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()" />&nbsp;
    </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();

    }
  
}

转载于:https://www.cnblogs.com/qinhaijun/archive/2011/08/26/2154388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值