asp.net中两个ListBox 左右互相移动,以及上移、下移

        





 

    //右移
    protected void btnLeftToRight_Click(object sender, EventArgs e)
    {
        //定义中间动态存储  
        ArrayList arrRight = new ArrayList();
        //读取左边listbox的item的选中项  
        foreach (ListItem item in this.leftListBox.Items)
        {
            if (item.Selected)
            {
                arrRight.Add(item);
            }
        }
        //执行右移操作  
        foreach (ListItem item in arrRight)
        {
            this.rightListBox.Items.Add(item);
            this.leftListBox.Items.Remove(item);
        }  
    }

    //左移
    protected void btnRightToLeft_Click(object sender, EventArgs e)
    {
        ArrayList arrLeft = new ArrayList();
        //读取右边listboxitem的选中项  
        foreach (ListItem item in this.rightListBox.Items)
        {
            if (item.Selected)
            {
                arrLeft.Add(item);
            }
        }
        //执行左移操作  
        foreach (ListItem item in arrLeft)
        {
            this.leftListBox.Items.Add(item);
            this.rightListBox.Items.Remove(item);
        }  
    }

    //上移
    protected void btnRightUp_Click(object sender, EventArgs e)
    {
        try
        {
            if (rightListBox.SelectedIndex > 0)
            {
                int idx = rightListBox.SelectedIndex;
                var SelectedItem = rightListBox.SelectedItem;
                rightListBox.Items.Insert(rightListBox.SelectedIndex - 1, new ListItem(SelectedItem.Text, SelectedItem.Value));
                rightListBox.Items.RemoveAt(rightListBox.SelectedIndex);
                rightListBox.SelectedIndex = idx - 1;
            }
        }
        catch (Exception)
        {
            Response.Write("");
        }  
    }

    //下移
    protected void btnRightDown_Click(object sender, EventArgs e)
    {
        try
        {
            if (rightListBox.SelectedIndex < rightListBox.Items.Count - 1)
            {
                int idx = rightListBox.SelectedIndex;
                rightListBox.Items.Insert(rightListBox.SelectedIndex, rightListBox.Items[rightListBox.SelectedIndex + 1]);
                rightListBox.Items.RemoveAt(rightListBox.SelectedIndex + 1);
                rightListBox.SelectedIndex = idx + 1;
            }
        }
        catch (Exception)
        {
            Response.Write("");
        }  
    }


 

转载于:https://www.cnblogs.com/smartsmile/archive/2012/10/31/6234397.html

你可能感兴趣的:(asp.net中两个ListBox 左右互相移动,以及上移、下移)