ASP.NET ListBoxコンテンツのコピーと削除
3369 ワード
フロントコードは、ごく普通のページです.ページには2つのListboxボタンと2つのbuttonボタンがあります.
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<listbox id="ListBox1" runat="server" selectionmode="Multiple"/>
</td>
<td>
<button id="Button1" runat="server" text="》》"/><br/>
<br/>
<button id="Button2" runat="server" text="《《"/>
</td>
<td>
<listbox id="ListBox2" runat="server" selectionmode="Multiple"/>
</td>
</tr>
</table>
</div>
</form>
</code></pre>
<p><br/> </p>
<p> :</p>
<pre><code>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 name="e"/>
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 name="e"/>
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();
}
}
</code></pre>
<p><br/> ListBox SelectionMode="Multiple" 。<br/></p>
</div>
</div>
</div>
</div>