两个listbox,点击按钮将左边选中的插入到右边,点击另一个按钮又把右边的删除,然后点击第三个按钮取出第二个listbox里所有的值。protected void Button1_Click(object sender, EventArgs e)//添加到新listbox
    {
        for (int k = 0; k < ListBox1.Items.Count; k++)
        {
            if ((ListBox1.Items[k].Selected) && (ListBox4.Items.FindByValue(ListBox1.Items[k].Value) == null))
            {
                ListItem item = ListBox1.Items[k];
                ListBox4.Items.Add(item);
            }
        }
    }
以上代码没有作用啊,请大大帮忙!!

解决方案 »

  1.   

    ListBox4.Items.FindByValue(ListBox1.Items[k].Value)
    ==========
    ListBox4.Items.FindByValue(ListBox1.SelectedValue)
      

  2.   

    http://blog.csdn.net/david_520042/archive/2008/12/24/3594856.aspx
      

  3.   

    大大,请继续帮忙,ListBox4.Items.Add(item);这句要如何修改??删除的代码如下protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
            {
                int total = this.ListBox4.Items.Count;
                int index=0;
                for (int k = 0; k < total; k++)
                {
                    ListItem item = this.ListBox4.Items[index];
                    if (this.ListBox4.Items[index].Selected)
                    {
                        this.ListBox4.Items.Remove(item);
                        index--;//listbox删除一行后,后面的一行将自动移上去
                    }
                    index++;
                }
            }如何修改啊?
      

  4.   

    给我自己顶一下,网上看了许多listbox的用法,都没有作用啊,很纳闷,,, 求大大解决
      

  5.   

    懒得写
    JS实现,几个重要的点
    移动
    var v = lst1.options[lstindex].value;
    var t = lst1.options[lstindex].text;
    lst2.options[lst2.options.length] = new Option(t,v,true,true);删除
    var lstindex=lst2.selectedIndex;
    lst2.options[lstindex].parentNode.removeChild(lst2.options[lstindex]);取值
    var length = lib.options.length;
    for (var i = 0; i < length; i++) {
        strText = strText + lib.options[i].innerText + ",";
        strValue = strValue + lib.options[i].value + ",";
    }
      

  6.   

    if (lst1.SelectedIndex != -1)
    {
      for (int i = 0; i < lst1.SelectedItems.Count; i++)
      {
      }

    string listBox1 = this.ListBox1.SelectedItem.Text;
     this.ListBox2.Items.Add(listBox1);
     this.ListBox1.Items.Remove(listBox1); <asp:ListBox ID="lst_left" runat="server" SelectionMode="Multiple" Height="203px"
      Width="130px"></asp:ListBox>
     <asp:ListBox ID="lst_center" runat="server" SelectionMode="Multiple" Height="203px" Width="130px"></asp:ListBox>
      <input type="button" id="btn_leftToCenter" value="右移" onclick="MoveOption(document.getElementById('lst_left'),document.getElementById('lst_center'))" />
      <input type="button" id="btn_centerToLeft" value="左移" onclick="MoveOption(document.getElementById('lst_center'),document.getElementById('lst_left'))" />function MoveOption(fromSel,toSel)
    {
      var fromOp=fromSel.options;
      var toOp=toSel.options;
      var selectedFlag=false;
      for(var i=0;i<fromOp.length;i++)
      {
      if(fromOp[i].selected)
      {
      selectedFlag=true;
      var selectedOp=document.createElement("option");
      selectedOp.text=fromOp[i].text;
      selectedOp.value=fromOp[i].value;
        
      toSelLength=toOp.length;
      toSel.options.add(selectedOp,toSelLength);
        
      fromSel.options.remove(i);
      i--;
      }
      }
      if(!selectedFlag)
      {
      alert("请选择一项!");
      return;
      }
    }
      

  7.   

    以前自己用的 仅做参考 左边的ListBox id=ListBoxDad 右边的ListBox id=ListBoxSon
    protected void Button1_Click(object sender, EventArgs e)                                 //右移事件 左边的向右边的移
        {
                int str = ListBoxDad.Items.Count;
                if (str > 0)
                {
                    for (int i = 0; i < str; i++)
                    {
                        //Response.Write((bool)ListBoxSon.Items[i].Selected);
                        if (ListBoxDad.SelectedIndex != -1)
                        {
                            string s_t = ListBoxDad.SelectedItem.Text;
                            string s_v = ListBoxDad.SelectedItem.Value;                        ListBoxSon.Items.Add(new ListItem(s_t, s_v));
                            ListBoxDad.Items.Remove(ListBoxDad.SelectedItem);                    }
                    }            }
                else
                {
                    UserClass userclass = new UserClass();
                    userclass.Alert("alert('都没东西,叫我移什么啊!')");
                }    
        }
    左边的ListBox内部移动
        protected void Button3_Click(object sender, EventArgs e)                                //上移位事件
        {        int index = -1;
            int str = ListBoxDad.Items.Count;
            UserClass userclass = new UserClass();
            if (str > 0)
            {
                if (ListBoxDad.SelectedIndex != -1)
                {
                    if (ListBoxDad.SelectedIndex != 0)
                    {
                        ListItem listitem = new ListItem(ListBoxDad.SelectedItem.Text, ListBoxDad.SelectedItem.Value);
                        ListBoxDad.Items[ListBoxDad.SelectedIndex].Text = ListBoxDad.Items[ListBoxDad.SelectedIndex + index].Text;
                        ListBoxDad.Items[ListBoxDad.SelectedIndex].Value = ListBoxDad.Items[ListBoxDad.SelectedIndex + index].Value;
                        ListBoxDad.Items[ListBoxDad.SelectedIndex + index].Text = listitem.Text;
                        ListBoxDad.Items[ListBoxDad.SelectedIndex + index].Value = listitem.Value;
                    }
                    else
                    {
                        userclass.Alert("alert('已经是最上面的,请选择别的!')");
                    }
                }
            }    }
        protected void Button4_Click(object sender, EventArgs e)                                //下移位事件
        {
            int index = 1;     //定义下移变量
            int str = ListBoxDad.Items.Count;                                   
            UserClass userclass = new UserClass();
            if (str > 0)
            {
                if (ListBoxDad.SelectedIndex != -1)
                {
                    if (ListBoxDad.SelectedIndex < (str - 1))
                    {
                        ListItem listitem = new ListItem(ListBoxDad.SelectedItem.Text, ListBoxDad.SelectedItem.Value);
                        ListBoxDad.Items[ListBoxDad.SelectedIndex].Text = ListBoxDad.Items[ListBoxDad.SelectedIndex + index].Text;
                        ListBoxDad.Items[ListBoxDad.SelectedIndex].Value = ListBoxDad.Items[ListBoxDad.SelectedIndex + index].Value;
                        ListBoxDad.Items[ListBoxDad.SelectedIndex + index].Text = listitem.Text;
                        ListBoxDad.Items[ListBoxDad.SelectedIndex + index].Value = listitem.Value;
                    }
                    else
                    {
                        userclass.Alert("alert('已经是最下面的,请选择别的!')");
                    }
                }
            }
        }
      

  8.   


     <asp:ListBox ID="ListBox1" runat="server" Height="182px" SelectionMode="Multiple"
                    Width="113px">
                    <asp:ListItem>北京</asp:ListItem>
                    <asp:ListItem>江苏</asp:ListItem>
                    <asp:ListItem>浙江</asp:ListItem>
                </asp:ListBox>
                <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="添加" />
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="删除" />
                <asp:Button ID="Button3" runat="server" Text="取值" OnClick="Button3_Click" />
                <asp:ListBox ID="ListBox2" runat="server" Height="179px" SelectionMode="Multiple"
                    Width="121px"></asp:ListBox>
        protected void Button2_Click(object sender, EventArgs e)//移动按钮事件
        {
            
         
         ListBox2.Items.Add(ListBox1.SelectedItem);//将listbox1中的选中项移到listbox2中
                }
        protected void Button1_Click1(object sender, EventArgs e)//删除按钮事件
        {
            ListBox2.Items.RemoveAt(ListBox2.SelectedIndex);//删除listbox2中的选中项
        }
        protected void Button3_Click(object sender, EventArgs e)//取值
        {
            string[] arr = new string[ListBox2.Items.Count];//ListBox2.Items.Count获得项数
            for (int i = 0; i < ListBox2.Items.Count; i++)//循环取出每项的值
            {
                arr[i] = ListBox2.Items[i].Text;
            }
            foreach (string li in arr)//循环输出每项的值
                Response.Write(li);
        }
    实现方法如上,具体的细节方面楼主自己添加