ASP.Net两个ListBox控件的上移和下移

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




实现效果:

实现效果

ListBox.aspx.cs代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ListBox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ArrayList list = new ArrayList();
            for (int i = 0; i <= 10; i++)
            {
                list.Add(i.ToString());
            }
            ListBox1.DataSource = list;
            ListBox1.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ListBox1.SelectedIndex > 0 && ListBox1.SelectedIndex <= ListBox1.Items.Count - 1)
        {
            string name = ListBox1.SelectedItem.Text;
            string value = ListBox1.SelectedItem.Value;
            int index = ListBox1.SelectedIndex;
            ListBox1.SelectedItem.Text = ListBox1.Items[index - 1].Text;
            ListBox1.SelectedItem.Value = ListBox1.Items[index - 1].Value;
            ListBox1.Items[index - 1].Text = name;
            ListBox1.Items[index - 1].Value = value;
            ListBox1.SelectedIndex--;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (ListBox1.SelectedIndex >= 0 && ListBox1.SelectedIndex < ListBox1.Items.Count - 1)
        {
            string name = ListBox1.SelectedItem.Text;
            string value = ListBox1.SelectedItem.Value;
            int index = ListBox1.SelectedIndex;
            ListBox1.SelectedItem.Text = ListBox1.Items[index + 1].Text;
            ListBox1.SelectedItem.Value = ListBox1.Items[index + 1].Value;
            ListBox1.Items[index + 1].Text = name;
            ListBox1.Items[index + 1].Value = value;
            ListBox1.SelectedIndex++;
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (ListBox1.SelectedIndex > 0 && ListBox1.SelectedIndex <= ListBox1.Items.Count - 1)
        {
            string name = ListBox1.SelectedItem.Text;
            string value = ListBox1.SelectedItem.Value;
            int index = 0;
            ListBox1.SelectedItem.Text = ListBox1.Items[0].Text;
            ListBox1.SelectedItem.Text = ListBox1.Items[0].Value;
            ListBox1.Items[index].Text = name;
            ListBox1.Items[index].Value = value;
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        int count = ListBox1.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox1.Items[index];
            if (ListBox1.Items[index].Selected == true)
            {
                ListBox1.Items.Remove(item);
                index--;
            }
            index++;
        }
    }
}

原文链接:https://www.idaobin.com/archives/1126.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

+ 2 = 3