ASP.NET ListBox内容复制和删除

来源:互联网 发布:怎样注销阿里云账号 编辑:程序博客网 时间:2024/06/07 02:03

前台代码,仅仅是很普通的一个页面。页面中存在2个Listbox和2个button按钮:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <table>            <tr>                <td>                    <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox>                </td>                <td>                    <asp:Button ID="Button1" runat="server" Text="》》" onclick="Button1_Click" /><br />                    <br />                    <asp:Button ID="Button2" runat="server" Text="《《" onclick="Button2_Click" />                </td>                <td>                    <asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple"></asp:ListBox>                </td>            </tr>        </table>    </div>    </form></body></html>


 

后天代码:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Bll;public partial class _Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            //这里是我自己写的绑定数据源            ListBox1.DataSource = BllEntity.GetAllCity();            ListBox1.DataTextField = "CityName";            ListBox1.DataValueField = "Id";            ListBox1.Rows = BllEntity.GetAllCity().Count + 1;            ListBox2.Rows = BllEntity.GetAllCity().Count + 1;            ListBox1.DataBind();        }    }    /// <summary>    /// 将左边的Listbox选中项 移动到右侧。允许选中多条    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    protected void Button1_Click(object sender, EventArgs e)    {        for (int i = 0; i < ListBox1.Items.Count - 1; i++)        {            if (ListBox1.Items[i].Selected)            {                for (int j = 0; j < ListBox2.Items.Count; j++)                {                    if (ListBox2.Items[j].Value == ListBox1.Items[i].Value)                    {                        ListBox2.Items.Remove(ListBox2.Items[j]);                    }                }                ListBox2.Items.Add(ListBox1.Items[i]);            }        }        ListBox2.ClearSelection();    }    /// <summary>    /// 将右侧的Listbox选中项从中删除掉    /// 注意,此处用的是递减for循环。因为没删除一个子项,Listbox的Items都会减少1个    /// 具体为什么这么写,大家自己考虑    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    protected void Button2_Click(object sender, EventArgs e)    {        ListItem[] lstItem = new ListItem[ListBox2.Items.Count];        for (int i = ListBox2.Items.Count - 1; i >= 0; i--)        {            if (ListBox2.Items[i].Selected)            {                ListBox2.Items.Remove(ListBox2.Items[i]);            }        }        ListBox2.ClearSelection();    }}


ListBox的 这个属性SelectionMode="Multiple"  是启用多选状态。

原创粉丝点击