1。
循环遍历 + 判断2。
DEMO<%@ Page Language="C#" AutoEventWireup="true"  %><%--http://community.csdn.net/Expert/TopicView3.asp?id=5619363--%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    protected void Button1_Click(object sender, EventArgs e)
    {
        SwapListBox(ListBox1, ListBox2);       
    }    protected void Button2_Click(object sender, EventArgs e)
    {
        SwapListBox(ListBox2, ListBox1);
    }    private void SwapListBox(ListBox src, ListBox dst)
    {
        for (int i = 0; i < src.Items.Count; ) {
            ListItem item = src.Items[i];
            if (item.Selected) {
                src.Items.Remove(item);
                item.Selected = (dst.SelectionMode == ListSelectionMode.Multiple ? item.Selected : false);
                dst.Items.Add(item);
                continue;
            }
            i++;
        }
    }
</script><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Double ListBox</title>
    <script type="text/javascript">
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div style="float:left">
        <asp:ListBox ID="ListBox1" BorderWidth="1" runat="server" SelectionMode="Multiple">
            <asp:ListItem Value="Item1">Item1</asp:ListItem>
            <asp:ListItem Value="Item2">Item2</asp:ListItem>
            <asp:ListItem Value="Item3">Item3</asp:ListItem>
            <asp:ListItem Value="Item4">Item4</asp:ListItem>
        </asp:ListBox>
    </div>
    <div style="float:left;vertical-align:middle;">
        <asp:Button ID="Button1" runat="server" Text=">>" OnClick="Button1_Click" />
        <br />
        <asp:Button ID="Button2" runat="server" Text="<<" OnClick="Button2_Click" />
    </div>
    <div>
       <asp:ListBox ID="ListBox2" BorderWidth="1" runat="server">            
        </asp:ListBox>
    </div>
    </div>
    </form>
</body>
</html>

解决方案 »

  1.   

    上面的写法在ListSelectionMode=single的时候,移出最后一项的时候会有问题。应该先添加到目的ListBox,然后在从本ListBox清除。而且,在Multiple模式下移动,还是有很多问题的。
      

  2.   

    给你一段代码:从A中选中放到B中,用循环,每移出一个,则源的个数减一。
     private void AddLstItems(ListBox lstValue, ListBox lstValueSelected, string strValue, string strText)
        {
            string[] arrValue = new string[] { };
            string[] arrText = new string[] { };
            arrValue = strValue.Trim().Split(';');
            arrText = strText.Trim().Split(';');
            lstValueSelected.Items.Clear();
            for (int i = 0; i < arrValue.Length; i++)
            {
                ListItem newItem = new ListItem();
                newItem.Value = arrValue[i].Trim();
                newItem.Text = arrText[i].Trim();
                lstValueSelected.Items.Add(newItem);
                for (int j = 0; j < lstValue.Items.Count; j++)
                {
                    if (arrText[i].Trim() == lstValue.Items[j].Text.Trim())
                    {
                        lstValue.Items.RemoveAt(j);
                        j--;
                        break;
                    }
                }
            }
        }
      

  3.   

    上面的写法在ListSelectionMode=single的时候,移出最后一项的时候会有问题。应该先添加到目的ListBox,然后在从本ListBox清除。而且,在Multiple模式下移动,还是有很多问题的。===========有问题???我不知道,你测试过没有?能告诉我出错的异常信息吗?
      

  4.   

    哦,再看了下,Jinglecat的代码在Multiple模式的时候,多行移出是对的。只是应该先添加到目的表,后剔除源表。
      

  5.   

    /// <summary>
        /// 从一个列表里移到另一个列表里(一项或多项)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btndownMove_Click(object sender, EventArgs e)
        {
            if (LbUp.SelectedIndex == -1)
            {
                Response.Write("<script>window.alert('请选择要移动的项')</script>");
            }
            while (LbUp.SelectedIndex != -1)
            {
                ListItem listItem = new ListItem(LbUp.SelectedItem.ToString(), LbUp.SelectedValue.ToString());
                LbUp.Items[LbUp.SelectedIndex].Selected = false;
                if (LbDown.Items.FindByValue(listItem.Value) == null)
                { 
                    LbDown.Items.Add(listItem);
                }
                LbUp.Items.Remove(listItem);
            }
            LbDown.SelectedIndex = -1;        if (LbDown.Items.Count > 0)
            {
                LbDown.Items[LbDown.Items.Count - 1].Selected = true;
            }
        }
      

  6.   

    lbUp就是你所说的ListBoxA
    lbDown 就是ListBoxB
      

  7.   

    你们回发服务吗.不回发服务器的话是不行的.因为你改变了他们里面的值.asp.net有个什么验证阻止你这样做的,如果回发服务器就好办了Item ListBox1.SelectItemListBox1.Items.Remove(item)ListBox2.Items.Add(item)
      

  8.   

    哦,再看了下,Jinglecat的代码在Multiple模式的时候,多行移出是对的。只是应该先添加到目的表,后剔除源表。
    ==========
    你错了,应该先移出(Remove),使其不属于任意一个 ListBox,然后将它添加到 另一个 ListBox,逻辑上,你想想,怎么可以有一个 ListItem 同时属于两个 ListBox 呢?当然,这里 ListBox 内部实际上没有做这项检验工作,
    即使你将一个 ListItem 同时加到两个LixtBox 它也一样工作
      

  9.   

    我有个比较傻瓜的..
    看看行不?
    private void Button1_Click(object sender, System.EventArgs e)
    {
    if(ListBox1.SelectedItem!=null)
    {
    ListBox2.Items.Add(ListBox1.SelectedValue);
    ListBox1.Items.Remove(ListBox1.SelectedValue);
    }
    } private void Button2_Click(object sender, System.EventArgs e)
    {
    if(ListBox2.SelectedItem!=null)
    {
    ListBox1.Items.Add(ListBox2.SelectedValue);
    ListBox2.Items.Remove(ListBox2.SelectedValue);
    }
    }
      

  10.   

    google 一个叫做 DualList 的东西
      

  11.   

    我有javascript +ajax的实例,哪个要来的?