使用两个ListBox实现左移、右移、清除的功能

1510阅读 0评论2013-08-14 lwyyuan
分类:C#/.net

做一个小例子,实现两个listbox的数据实现“左移”,“右移”、“清除”某一listbox的数据的功能。
前台两个listbox的控件代码如下:
      

            ListBox1

       

       

       

            ListBox2

       

           
           

       

       

                            Style="background: url(../images/s_bgs.png) repeat-x 0 -118px; width: 100px;
                border-color: #7F9DB9 #7F9DB9 #7F9DB9; border-right: 1px solid #7F9DB9; border-style: solid;
                border-width: 1px; box-shadow: 0 1px 1px #FFFFFF; display: block; float: left;
                vertical-align: top; position: relative; top: 110px; left: 15px; padding: 0;
                margin: 0;" />
           
           
       

       

           
       



后台cs方法如下:

//清楚全部
    protected void btnClear_Click(object sender, EventArgs e)
    {
        int count = listSend.Items.Count;
        int index = 0;

        for (int i = 0; i < count; i++)
        {
            ListItem item = listSend.Items[index];
            listSend.Items.Remove(item);
        }
        index++;
    }
    //右移
    protected void btnAddRight_Click(object sender, EventArgs e)
    {
        if (listYuan.SelectedIndex <= -1)
        {
            Common.ShowAlter(this.Page, "提示", "请选择要要右移的数据");
            return;
        }

        int count = listYuan.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = listYuan.Items[index];
            if (item.Selected == true)
            {

                listYuan.Items.Remove(item);
                listSend.Items.Add(item);
                this.listSend.SelectedIndex = (listSend.Items.Count - 1);
                index--;
            }
            index++;
        }

    }
    //左移
    protected void btnAddLeft_Click(object sender, EventArgs e)
    {
        int count = listSend.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = listSend.Items[index];
            if (item.Selected == true)
            {

                listSend.Items.Remove(item);
                listYuan.Items.Add(item);
                this.listYuan.SelectedIndex = (this.listYuan.Items.Count - 1);
                index--;
            }
            index++;
        }
    }

上一篇:asp.net中生成验证图片方法
下一篇:简单设置U盘只读方法