.NET上下左右移动操作支持多选

效果如下图:

代码如下:

    public partial class FieldShowSet : System.Web.UI.Page
    {
        Hashtable resources = EquStatusSearch.resources;
        private string names = null;
        IniOperation ini = new IniOperation("i18n");


        Hashtable leftSelectItems = new Hashtable(); //左边选中项集合
        Hashtable rightSelectItems = new Hashtable(); //右边选中项集合

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                names = QuarrysClass.All;
                initi18n();

                string[] all = string.IsNullOrEmpty(names) ? null : names.Split(',');//分割出所有列
                hidfShows.Value = ini.IniValue("Set", "Show");
                string include = hidfShows.Value;//获取当前页展示列
                if (string.IsNullOrEmpty(names) || all == null)
                {
                    //请先点加载配置信息
                    return;
                }
                for (int i = 0; i < all.Length; i++)//遍历所有列
                {
                    if (include.IndexOf("@" + all[i] + "@") == -1)//当前列中不包含分割出来的列时放在不显示框中
                    {
                        leftListBox.Items.Add(new ListItem(resources[all[i]].ToString(), all[i]));
                    }
                }
                string[] showNames = include.Split(',');
                string newName = string.Empty;
                foreach (string name in showNames)
                {
                    newName = name.Trim('@');
                    string colName = resources[newName] == null ? string.Empty : resources[newName].ToString();
                    if (!string.IsNullOrEmpty(newName))
                    {
                        rightListBox.Items.Add(new ListItem(colName, newName));
                    }
                }
            }
        }

        //调用显示和展示字段名称
        private void initi18n()
        {
            this.Page.Title = string.IsNullOrEmpty(resources["ShowSetName"].ToString()) ? "字段显示设置" : resources["ShowSetName"].ToString();
            xLabel1.Text = string.IsNullOrEmpty(resources["ShowSetNoShow"].ToString()) ? "不显示字段" : resources["ShowSetNoShow"].ToString();
            xLabel2.Text = string.IsNullOrEmpty(resources["ShowSetShow"].ToString()) ? "显示字段" : resources["ShowSetShow"].ToString();
        }

        //将未显示字段 右移 到显示字段框,并清除掉不显示的选择到的项
        protected void rbtnMove_Click(object sender, EventArgs e)
        {
            if (leftListBox.SelectedItem == null)
            {
                return;
            }
            leftSelectItems.Clear();
            foreach (ListItem item in leftListBox.Items)
            {
                if (item.Selected)
                {
                    if (!leftSelectItems.ContainsKey(item.Value))
                    {
                        leftSelectItems.Add(item.Value, item.Text);
                    }
                }
            }
            foreach (DictionaryEntry v in leftSelectItems)
            {
                leftListBox.Items.Remove(new ListItem(v.Value.ToString(),v.Key.ToString()));
                rightListBox.Items.Add(new ListItem(v.Value.ToString(), v.Key.ToString()));
            }
        }

        //将显示字段 左移 到未显示字段框,并清除掉显示的选择到的项
        protected void lbtnMove_Click(object sender, EventArgs e)
        {
            if (rightListBox.SelectedItem == null)
            {
                return;
            }
            rightSelectItems.Clear();
            foreach (ListItem item in rightListBox.Items)
            {
                if (item.Selected)
                {
                    if (!rightSelectItems.ContainsKey(item.Value))
                    {
                        rightSelectItems.Add(item.Value, item.Text);
                    }
                }
            }
            foreach (DictionaryEntry v in rightSelectItems)
            {
                rightListBox.Items.Remove(new ListItem(v.Value.ToString(), v.Key.ToString()));
                leftListBox.Items.Add(new ListItem(v.Value.ToString(), v.Key.ToString()));
            }
        }

        //将左边所有不显示项加到右边的显示项中
        protected void btnAllLMove_Click(object sender, EventArgs e)
        {
            foreach (ListItem item in leftListBox.Items)
            {
                rightListBox.Items.Add(item);
            }
            leftListBox.Items.Clear();
        }

        //将右边所有不显示项加到左边的显示项中
        protected void btnAllRMove_Click(object sender, EventArgs e)
        {
            foreach (ListItem item in rightListBox.Items)
            {
                leftListBox.Items.Add(item);
            }
            rightListBox.Items.Clear();
        }


        //点击设置按钮事件,
        protected void btnSet_Click(object sender, EventArgs e)
        {
            try
            {
                string str = "";
                for (int i = 0; i < rightListBox.Items.Count; i++)//循环添加显示项组成字符串
                {
                    str += "@" + rightListBox.Items[i].Value + "@,";
                }
                if ("".Equals(str))//如果显示项为空直接清除显示页面的所有字段
                {
                    QuarrysClass.Shows = "";
                }
                else
                {
                    //重新复制显示字段到显示页面
                    QuarrysClass.Shows = str.Substring(0, str.Length - 1);
                }
                //调用主页面将显示字段写入到i18n文件中
                ini.IniWriteValue("Set", "Show", QuarrysClass.Shows);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "warn", "alert('设置成功!');window.opener.searchData();window.close();", true);

            }
            catch (Exception ex)
            {
                string strScript = string.Format("alert('设置失败!{0}');", ex.Message);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "warn", strScript, true);
            }
        }

        /*************************************************************************/

        /// <summary>
        /// 得到选定项的值
        /// </summary>
        /// <returns></returns>
        protected string[] getSelectedValue()
        {
            ArrayList a = new ArrayList();
            for (int i = 0; i < rightListBox.Items.Count; i++)
            {
                if (rightListBox.Items[i].Selected)
                {
                    a.Add(i);
                }
            }
            int[] indices = new int[a.Count];  //得到选中项的索引集合
            a.CopyTo(indices);

            string[] strs = new string[indices.Length];
            for (int i = 0; i < indices.Length; i++)
            {
                strs[i] = rightListBox.Items[indices[i]].Value;
            }

            return strs;
        }

        /// <summary>
        /// 得到选定项的内容
        /// </summary>
        /// <returns></returns>
        protected string[] getSelectedText()
        {
            ArrayList a = new ArrayList();
            for (int i = 0; i < rightListBox.Items.Count; i++)
            {
                if (rightListBox.Items[i].Selected)
                {
                    a.Add(i);
                }
            }
            int[] indices = new int[a.Count];  //得到选中项的索引集合
            a.CopyTo(indices);
            string[] strs = new string[indices.Length];
            for (int i = 0; i < indices.Length; i++)
            {
                strs[i] = rightListBox.Items[indices[i]].Text;
            }
            return strs;
        }


        //上移
        protected void btnUp_Click(object sender, EventArgs e)
        {
            UpMove();
        }

        //下移
        protected void btnDown_Click(object sender, EventArgs e)
        {
            DownMove();
        }

        void UpMove()
        {
            if (rightListBox.SelectedIndex > 0)
            {
                string[] newValue = getSelectedValue();
                string[] newText = getSelectedText();

                ArrayList a = new ArrayList();
                for (int i = 0; i < rightListBox.Items.Count; i++)
                {
                    if (rightListBox.Items[i].Selected)
                    {
                        a.Add(i);
                    }
                }
                int[] indices = new int[a.Count];  //得到选中项的索引集合
                a.CopyTo(indices);

                for (int i = 0; i < indices.Length; i++)
                {
                    int newIndex = indices[i];
                    //交换位置
                    rightListBox.Items[newIndex].Value = rightListBox.Items[newIndex - 1].Value;
                    rightListBox.Items[newIndex].Text = rightListBox.Items[newIndex - 1].Text;
                    rightListBox.Items[newIndex - 1].Value = newValue[i];
                    rightListBox.Items[newIndex - 1].Text = newText[i];

                    rightListBox.Items[newIndex - 1].Selected = true;
                    rightListBox.Items[newIndex].Selected = false;
                }
                rightListBox.Items[indices[indices.Length - 1]].Selected = false;
            }
        }

        void DownMove()
        {
            string[] newValue = getSelectedValue();
            string[] newText = getSelectedText();

            ArrayList a = new ArrayList();
            for (int i = 0; i < rightListBox.Items.Count; i++)
            {
                if (rightListBox.Items[i].Selected)
                {
                    a.Add(i);
                }
            }
            int[] indices = new int[a.Count];  //得到选中项的索引集合
            a.CopyTo(indices);

            if (indices[a.Count - 1] < rightListBox.Items.Count - 1)  //选中项的最大索引小于列表总项数
            {
                for (int i = indices.Length - 1; i >= 0; i--)
                {
                    int newIndex = indices[i];

                    //交换位置
                    rightListBox.Items[newIndex].Value = rightListBox.Items[newIndex + 1].Value;
                    rightListBox.Items[newIndex].Text = rightListBox.Items[newIndex + 1].Text;
                    rightListBox.Items[newIndex + 1].Value = newValue[i];
                    rightListBox.Items[newIndex + 1].Text = newText[i];

                    rightListBox.Items[newIndex + 1].Selected = true;
                    rightListBox.Items[newIndex].Selected = false;
                }
                rightListBox.Items[indices[0]].Selected = false;
            }
        }

        void TopMove()
        {
            string[] newValue = getSelectedValue();
            string[] newText = getSelectedText();

            ArrayList a = new ArrayList();
            for (int i = 0; i < rightListBox.Items.Count; i++)
            {
                if (rightListBox.Items[i].Selected)
                {
                    a.Add(i);
                }
            }
            int[] indices = new int[a.Count];  //得到选中项的索引集合
            a.CopyTo(indices);

            for (int i = 0; i < indices.Length; i++)
            {
                int newIndex = indices[i];

                for (int j = newIndex - 1; j >= 0; j--)
                {
                    rightListBox.Items[j + 1].Text = rightListBox.Items[j].Text;
                    rightListBox.Items[j + 1].Value = rightListBox.Items[j].Value;
                }
                rightListBox.Items[0].Value = newValue[indices.Length - 1 - i];
                rightListBox.Items[0].Text = newText[indices.Length - 1 - i];

                rightListBox.Items[newIndex].Selected = false;
                rightListBox.Items[i].Selected = true;
            }
        }

        void BottomMove()
        {
            string[] newValue = getSelectedValue();
            string[] newText = getSelectedText();

            ArrayList a = new ArrayList();
            for (int i = 0; i < rightListBox.Items.Count; i++)
            {
                if (rightListBox.Items[i].Selected)
                {
                    a.Add(i);
                }
            }
            int[] indices = new int[a.Count];  //得到选中项的索引集合
            a.CopyTo(indices);

            for (int i = indices.Length - 1; i >= 0; i--)
            {
                int newIndex = indices[i];

                //获取总的数目
                int newCount = rightListBox.Items.Count;

                for (int j = newIndex; j < newCount - 1; j++)
                {
                    rightListBox.Items[j].Text = rightListBox.Items[j + 1].Text;
                    rightListBox.Items[j].Value = rightListBox.Items[j + 1].Value;
                }
                rightListBox.Items[newCount - 1].Value = newValue[indices.Length - 1 - i];
                rightListBox.Items[newCount - 1].Text = newText[indices.Length - 1 - i];

                rightListBox.Items[newIndex].Selected = false;
                rightListBox.Items[newCount - 1 - (indices.Length - 1 - i)].Selected = true;
            }
        }

        protected void btnTop_Click(object sender, EventArgs e)
        {
            TopMove();
        }

        protected void btnBottom_Click(object sender, EventArgs e)
        {
            BottomMove();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邹琼俊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值