asp.net ListBox单选、全选、清除等功能

本文介绍了一个简单的C# Web应用实例,通过设置ListBox的SelectionMode属性为multiple,并利用事件处理实现了从一个ListBox向另一个ListBox的数据迁移操作。包括添加、删除、全选和全删的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:注意要得把两个listbox的selectectionmode属性设置为mutiple

2 后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TestListBox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            ListBox2Bind();
          
        }
    }

    private void ListBox2Bind()
    {
        string[] str = new string[] { "北京","上海","福建","广东","浙江" };
        for (int i = 0; i < str.Length - 1; i++)
        {
            ListBox2.Items.Add(str[i]);
        }

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        
        int count = ListBox2.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox2.Items[index];
            if (ListBox2.Items[index].Selected == true)
            {
                ListBox2.Items.Remove(item);
                ListBox3.Items.Add(item);
                index--;
            }
            index++;
        }
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        int count = ListBox3.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox3.Items[index];
            if (ListBox3.Items[index].Selected == true)
            {
                ListBox3.Items.Remove(item);
                ListBox2.Items.Add(item);
                index--;
            }
            index++;
        }

    }
    protected void AddAll_Click(object sender, EventArgs e)
    {
        int count = ListBox2.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox2.Items[index];
            ListBox2.Items.Remove(item);
            ListBox3.Items.Add(item);
        }
        index++;
    }
    protected void DelAll_Click(object sender, EventArgs e)
    {
        int count = ListBox3.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox3.Items[index];
            ListBox3.Items.Remove(item);
            ListBox2.Items.Add(item);
        }
        index++;
    }
}

 3:效果图

转载于:https://www.cnblogs.com/thbbsky/archive/2013/05/22/3092842.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值