左右两个列表框项之间的添加、移除、上下移动

左右两个列表框项之间的添加、移除、上下移动的JS脚本:
ContractedBlock.gif ExpandedBlockStart.gif Code
<script language="javascript" type="text/javascript">
// <!CDATA[
        
//双击source某项时添加该项到destination
ExpandedBlockStart.gifContractedBlock.gif
        function AddOne(source, destination) {
            var oOption 
= document.createElement("option");
            oOption.text 
= source.options[source.selectedIndex].text;
            oOption.value 
= source.options[source.selectedIndex].value;
            destination.options.add(oOption);
        }


        
//双击list某项时删除该项
ExpandedBlockStart.gifContractedBlock.gif
        function DeleteOne(list) {
            list.options.remove(list.selectedIndex);
        }


        
//将source选择项全部添加到destination
ExpandedBlockStart.gifContractedBlock.gif
        function selAdd(source, destination) {
            var count 
= source.options.length;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (var i = 0; i < count; i++{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (source.options[i].selected) {
                    var oOption 
= document.createElement("option");
                    oOption.text 
= source.options[i].text;
                    oOption.value 
= source.options[i].value;
                    destination.options.add(oOption);
                }

            }

        }


        
//将source所有项全部添加到destination
ExpandedBlockStart.gifContractedBlock.gif
        function selAddAll(source, destination) {
            var count 
= source.options.length;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (var i = 0; i < count; i++{
                var oOption 
= document.createElement("option");
                oOption.text 
= source.options[i].text;
                oOption.value 
= source.options[i].value;
                destination.options.add(oOption);
            }

        }


        
//将list选择项全部删除
ExpandedBlockStart.gifContractedBlock.gif
        function selDelete(list) {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (var i = list.options.length - 1; i >= 0; i--{
                
if (list.options[i].selected)
                    list.options.remove(i);
            }

        }


        
//删除list所有项
ExpandedBlockStart.gifContractedBlock.gif
        function selDeleteAll(list) {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (var i = list.options.length - 1; i >= 0; i--{
                list.options.remove(i);
            }

        }

        
//上移list某选择项
ExpandedBlockStart.gifContractedBlock.gif
        function selUp(list) {
            var i 
= list.selectedIndex;
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (i>0{
                    var t 
= list.options[i - 1].text;
                    var v 
= list.options[i - 1].value;
                    list.options[i 
- 1].text = list.options[i].text;
                    list.options[i 
- 1].value = list.options[i].value;
                    list.options[i].text 
= t;
                    list.options[i].value 
= v;
                    list.options[i].selected 
= false;
                    list.options[i 
- 1].selected = true;
                    
return;
                }

        }


        
//下移list某选择项
ExpandedBlockStart.gifContractedBlock.gif
        function selDown(list) {
            var i 
= list.selectedIndex;
            var count 
= list.options.length;
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (i<count-1{
                    var t 
= list.options[i].text;
                    var v 
= list.options[i].value;
                    list.options[i].text 
= list.options[i + 1].text;
                    list.options[i].value 
= list.options[i + 1].value;
                    list.options[i 
+ 1].text = t;
                    list.options[i 
+ 1].value = v;
                    list.options[i 
+ 1].selected = true;
                    list.options[i].selected 
= false;
                    
return;
                }

        }


        
//保存list列表框所有项到hf隐藏域
ExpandedBlockStart.gifContractedBlock.gif
        function SaveList(list, hf) {
            hf.value 
= "";
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (var i = 0; i < list.options.length; i++{
                hf.value 
+= list.options[i].text + ":" + list.options[i].value + "#";
            }

        }


ExpandedBlockStart.gifContractedBlock.gif
/**//////
        //双击lstSource列表框
ExpandedBlockStart.gifContractedBlock.gif
        function lstSource_dblclick() {
            var lstSource 
= document.getElementById("<%= lstSource.ClientID %>");
            var lstDestination 
= document.getElementById("<%= lstDestination.ClientID %>");
            AddOne(lstSource, lstDestination);
            DeleteOne(lstSource);
        }


        
//双击lstDestination列表框
ExpandedBlockStart.gifContractedBlock.gif
        function lstDestination_dblclick() {
            var lstSource 
= document.getElementById("<%= lstSource.ClientID %>");
            var lstDestination 
= document.getElementById("<%= lstDestination.ClientID %>");
            AddOne(lstDestination, lstSource);
            DeleteOne(lstDestination);
        }


        
//添加(从左往右)
ExpandedBlockStart.gifContractedBlock.gif
        function btnAdd_onclick() {
            var lstSource 
= document.getElementById("<%= lstSource.ClientID %>");
            var lstDestination 
= document.getElementById("<%= lstDestination.ClientID %>");
            selAdd(lstSource, lstDestination);
            selDelete(lstSource);
        }


        
//全部添加(从左往右)
ExpandedBlockStart.gifContractedBlock.gif
        function btnAddAll_onclick() {
            var lstSource 
= document.getElementById("<%= lstSource.ClientID %>");
            var lstDestination 
= document.getElementById("<%= lstDestination.ClientID %>");
            selAddAll(lstSource, lstDestination);
            selDeleteAll(lstSource);
        }


        
//移除(从右往左)
ExpandedBlockStart.gifContractedBlock.gif
        function btnDelete_onclick() {
            var lstSource 
= document.getElementById("<%= lstSource.ClientID %>");
            var lstDestination 
= document.getElementById("<%= lstDestination.ClientID %>");
            selAdd(lstDestination, lstSource);
            selDelete(lstDestination);
        }


        
//全部移除(从右往左)
ExpandedBlockStart.gifContractedBlock.gif
        function btnDeleteAll_onclick() {
            var lstSource 
= document.getElementById("<%= lstSource.ClientID %>");
            var lstDestination 
= document.getElementById("<%= lstDestination.ClientID %>");
            selAddAll(lstDestination, lstSource);
            selDeleteAll(lstDestination);
        }

        
        
//上移
ExpandedBlockStart.gifContractedBlock.gif
        function btnUp_onclick() {
            var obj 
= document.getElementById("<%= lstDestination.ClientID %>");
            selUp(obj);
        }

        
        
//下移
ExpandedBlockStart.gifContractedBlock.gif
        function btnDown_onclick() {
            var obj 
= document.getElementById("<%= lstDestination.ClientID %>");
            selDown(obj);
        }


        
//保存所有的列表框数据项到隐藏域
ExpandedBlockStart.gifContractedBlock.gif
        function SaveAllList() {
            var lstSource 
= document.getElementById("<%= lstSource.ClientID %>");
            var hfListSource 
= document.getElementById("<%= hfListSource.ClientID %>");
            SaveList(lstSource, hfListSource);
            var lstDestination 
= document.getElementById("<%= lstDestination.ClientID %>");
            var hfListDestination 
= document.getElementById("<%= hfListDestination.ClientID %>");
            SaveList(lstDestination, hfListDestination);
        }

// ]]>
    </script>
但是,通过JS脚本只是改变了列表框在客户端的各项内容,如果想保持列表框服务器端和客户端的内容一致,需借助HiddenField空间。页面元素代码如下:
ContractedBlock.gif ExpandedBlockStart.gif Code
<table>            
            
<tr>
                
<td>    
        
<asp:ListBox ID="lstSource" runat="server" Height="169px" Width="74px" 
                        SelectionMode
="Multiple" ondblclick="return lstSource_dblclick()">
        
</asp:ListBox>    
                
</td>
                
<td>
                    
<table class="style1">
                        
<tr>
                            
<td>
    
<input id="btnAdd" type="button" value="添加" onclick="return btnAdd_onclick()" /></td>
                        
</tr>
                        
<tr>
                            
<td>
    
<input id="btnAddAll" type="button" value="全部添加" onclick="return btnAddAll_onclick()" /></td>
                        
</tr>
                        
<tr>
                            
<td>
    
<input id="btnDelete" type="button" value="移除" onclick="return btnDelete_onclick()" /></td>
                        
</tr>
                        
<tr>
                            
<td>
    
<input id="btnDeleteAll" type="button" value="全部移除" onclick="return btnDeleteAll_onclick()" /></td>
                        
</tr>
                    
</table>
                
</td>
                
<td>
    
        
<asp:ListBox ID="lstDestination" runat="server" Height="169px" Width="74px" 
                        SelectionMode
="Multiple" ondblclick="return lstDestination_dblclick()">
        
</asp:ListBox>    
                
</td>
                
<td>
                    
<table class="style1">
                        
<tr>
                            
<td>
    
<input id="btnUp" type="button" value="上移" 
        onclick
="return btnUp_onclick()" /></td>
                        
</tr>
                        
<tr>
                            
<td>
                                
<input 
        id
="btnDown" type="button" value="下移" onclick="return btnDown_onclick()" /></td>
                        
</tr>
                    
</table>
                
</td>
            
</tr>
            
<tr>
                
<td>    
                    
&nbsp;</td>
                
<td align="center">
    
<asp:Button 
        ID
="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="确  定" 
                        OnClientClick
="SaveAllList()" UseSubmitBehavior="False" />
                
</td>
                
<td>
    
                    
&nbsp;</td>
                
<td>
                    
&nbsp;</td>
            
</tr>
    
</table>
    
        
<asp:HiddenField ID="hfListSource" runat="server" />    
        
<asp:HiddenField ID="hfListDestination" runat="server" />
后台代码CS文件的Page_Load事件代码如下:
ContractedBlock.gif ExpandedBlockStart.gif Code
protected void Page_Load(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
if (!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                BindSource();
                BindDestination();
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                lstSource.Items.Clear();
                
string[] list = hfListSource.Value.Split('#');
                
for (int i = 0; i < list.Length - 1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
string[] t = list[i].Split(':');
                    lstSource.Items.Add(
new ListItem(t[0], t[1]));
                }


                lstDestination.Items.Clear();
                list 
= hfListDestination.Value.Split('#');
                
for (int i = 0; i < list.Length - 1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
string[] t = list[i].Split(':');
                    lstDestination.Items.Add(
new ListItem(t[0], t[1]));
                }

            }
            
        }
posted on 2009-07-12 21:24 ms_dos 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/nine425/archive/2009/07/12/1522103.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值