ASP.NET 中listbox实现上移下移置顶置底,(支持多选)

/// <summary>
 /// 得到选定项的值
 /// </summary>
 /// <returns></returns>
 protected string[] getSelectedValue()
 {
  ArrayList a = new ArrayList();
  for (int i = 0; i < ListBoxNewPaper.Items.Count; i++)
  {
   if (ListBoxNewPaper.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] = ListBoxNewPaper.Items[indices[i]].Value;
  }

  return strs;
 }

 /// <summary>
 /// 得到选定项的内容
 /// </summary>
 /// <returns></returns>
 protected string[] getSelectedText()
 {
  ArrayList a = new ArrayList();
  for (int i = 0; i < ListBoxNewPaper.Items.Count; i++)
  {
   if (ListBoxNewPaper.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] = ListBoxNewPaper.Items[indices[i]].Text;
  }
  return strs;
 }

 /// <summary>
 /// 上移
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void btnUp_Click(object sender, EventArgs e)
 {
  if (ListBoxNewPaper.SelectedIndex > 0)
  {
   string[] newValue = getSelectedValue();
   string[] newText = getSelectedText();

   ArrayList a = new ArrayList();
   for (int i = 0; i < ListBoxNewPaper.Items.Count; i++)
   {
    if (ListBoxNewPaper.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];
    //交换位置
    ListBoxNewPaper.Items[newIndex].Value = ListBoxNewPaper.Items[newIndex - 1].Value;
    ListBoxNewPaper.Items[newIndex].Text = ListBoxNewPaper.Items[newIndex - 1].Text;
    ListBoxNewPaper.Items[newIndex - 1].Value = newValue[i];
    ListBoxNewPaper.Items[newIndex - 1].Text = newText[i];

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

 /// <summary>
 /// 下移
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void btnDown_Click(object sender, EventArgs e)
 {
  string[] newValue = getSelectedValue();
  string[] newText = getSelectedText();

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

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

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

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

 /// <summary>
 /// 置顶
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void btnTop_Click(object sender, EventArgs e)
 {
  string[] newValue = getSelectedValue();
  string[] newText = getSelectedText();

  ArrayList a = new ArrayList();
  for (int i = 0; i < ListBoxNewPaper.Items.Count; i++)
  {
   if (ListBoxNewPaper.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--)
   {
    ListBoxNewPaper.Items[j + 1].Text = ListBoxNewPaper.Items[j].Text;
    ListBoxNewPaper.Items[j + 1].Value = ListBoxNewPaper.Items[j].Value;
   }
   ListBoxNewPaper.Items[0].Value = newValue[indices.Length - 1 - i];
   ListBoxNewPaper.Items[0].Text = newText[indices.Length - 1 - i];

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

 /// <summary>
 /// 置底
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void btnFoot_Click(object sender, EventArgs e)
 {
  string[] newValue = getSelectedValue();
  string[] newText = getSelectedText();

  ArrayList a = new ArrayList();
  for (int i = 0; i < ListBoxNewPaper.Items.Count; i++)
  {
   if (ListBoxNewPaper.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 = ListBoxNewPaper.Items.Count;

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值