listbox 上下移动 (asp.net C#)

 protected void btnup_Click(object sender, EventArgs e)
    {

        if (lb_busspot.SelectedIndex == -1)
        {
            return;
        }

        //获得连续选中的项索引
        int[] Indices = lb_busspot.GetSelectedIndices();
        int length = Indices.Length;
        string text;
        string value;
        //如果选择的最小索引是0,表示是最上面的项
        if (Indices[0] == 0)
        {
            return;
        }

        //判断选择多项时是否是连续的项
        if (Indices.Length != 1 && Indices[0] + length - 1 != Indices[length - 1])
        {

            MessageBox.Show("Page", "请选择连续的项!");
            return;       
        }
        //将选中的上面一项未选中的值赋予临时变量
        text = lb_busspot.Items[Indices[0] - 1].Text;
        value = lb_busspot.Items[Indices[0] - 1].Value;

        for (int i = 0; i < length; i++)
        {
            lb_busspot.Items[Indices[i] - 1].Text = lb_busspot.Items[Indices[i]].Text;
            lb_busspot.Items[Indices[i] - 1].Value = lb_busspot.Items[Indices[i]].Value;
            //保证被选中状态
            lb_busspot.Items[Indices[i] - 1].Selected = true;
            lb_busspot.Items[Indices[i]].Selected = false;
        }
        //将选中的上面第一条未选中的值赋予到下面
        lb_busspot.Items[Indices[0] + length - 1].Text = text;
        lb_busspot.Items[Indices[0] + length - 1].Value = value;
    }

    protected void btndown_Click(object sender, EventArgs e)
    {
        if (lb_busspot.SelectedIndex==-1)
        {
            return;
        }
        //获得连续选中的项索引
        int[] Indices = lb_busspot.GetSelectedIndices();
        int length = Indices.Length;

        string text;
        string value;
        //如果选择的是最底下的项

        if (Indices[length-1]==lb_busspot.Items.Count-1)
        {
            return;
        }

        //判断选择多项时是否是连续的项

        if (Indices.Length !=1&&Indices[0]+length -1!=Indices[length -1])
        {
            MessageBox.Show("Page", "请选择连续的项!");
            return;
        }
        //将选中的下面一项未选中的值赋予临时变量
        text = lb_busspot.Items[Indices[length - 1] + 1].Text;
        value = lb_busspot.Items[Indices[length - 1] + 1].Value;
        for (int i = length; i > 0; i--)
        {
            lb_busspot.Items[Indices[i - 1] + 1].Text = lb_busspot.Items[Indices[i - 1]].Text;
            lb_busspot.Items[Indices[i - 1] + 1].Value = lb_busspot.Items[Indices[i - 1]].Value;
            lb_busspot.Items[Indices[i - 1] + 1].Selected = true;
            lb_busspot.Items[Indices[i - 1]].Selected = false;
        }
        //将下面第一条未选中的项的值赋予到上
        lb_busspot.Items[Indices[0]].Text = text;
        lb_busspot.Items[Indices[0]].Value = value;

    }

posted @ 2009-12-27 20:41  与时俱进  阅读(455)  评论(0编辑  收藏  举报
友情链接:同里老宅院民居客栈