1。
循环遍历 + 判断2。
DEMO<%@ Page Language="C#" AutoEventWireup="true" %><%--http://community.csdn.net/Expert/TopicView3.asp?id=5619363--%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Button1_Click(object sender, EventArgs e)
{
SwapListBox(ListBox1, ListBox2);
} protected void Button2_Click(object sender, EventArgs e)
{
SwapListBox(ListBox2, ListBox1);
} private void SwapListBox(ListBox src, ListBox dst)
{
for (int i = 0; i < src.Items.Count; ) {
ListItem item = src.Items[i];
if (item.Selected) {
src.Items.Remove(item);
item.Selected = (dst.SelectionMode == ListSelectionMode.Multiple ? item.Selected : false);
dst.Items.Add(item);
continue;
}
i++;
}
}
</script><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double ListBox</title>
<script type="text/javascript">
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="float:left">
<asp:ListBox ID="ListBox1" BorderWidth="1" runat="server" SelectionMode="Multiple">
<asp:ListItem Value="Item1">Item1</asp:ListItem>
<asp:ListItem Value="Item2">Item2</asp:ListItem>
<asp:ListItem Value="Item3">Item3</asp:ListItem>
<asp:ListItem Value="Item4">Item4</asp:ListItem>
</asp:ListBox>
</div>
<div style="float:left;vertical-align:middle;">
<asp:Button ID="Button1" runat="server" Text=">>" OnClick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server" Text="<<" OnClick="Button2_Click" />
</div>
<div>
<asp:ListBox ID="ListBox2" BorderWidth="1" runat="server">
</asp:ListBox>
</div>
</div>
</form>
</body>
</html>
循环遍历 + 判断2。
DEMO<%@ Page Language="C#" AutoEventWireup="true" %><%--http://community.csdn.net/Expert/TopicView3.asp?id=5619363--%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Button1_Click(object sender, EventArgs e)
{
SwapListBox(ListBox1, ListBox2);
} protected void Button2_Click(object sender, EventArgs e)
{
SwapListBox(ListBox2, ListBox1);
} private void SwapListBox(ListBox src, ListBox dst)
{
for (int i = 0; i < src.Items.Count; ) {
ListItem item = src.Items[i];
if (item.Selected) {
src.Items.Remove(item);
item.Selected = (dst.SelectionMode == ListSelectionMode.Multiple ? item.Selected : false);
dst.Items.Add(item);
continue;
}
i++;
}
}
</script><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double ListBox</title>
<script type="text/javascript">
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="float:left">
<asp:ListBox ID="ListBox1" BorderWidth="1" runat="server" SelectionMode="Multiple">
<asp:ListItem Value="Item1">Item1</asp:ListItem>
<asp:ListItem Value="Item2">Item2</asp:ListItem>
<asp:ListItem Value="Item3">Item3</asp:ListItem>
<asp:ListItem Value="Item4">Item4</asp:ListItem>
</asp:ListBox>
</div>
<div style="float:left;vertical-align:middle;">
<asp:Button ID="Button1" runat="server" Text=">>" OnClick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server" Text="<<" OnClick="Button2_Click" />
</div>
<div>
<asp:ListBox ID="ListBox2" BorderWidth="1" runat="server">
</asp:ListBox>
</div>
</div>
</form>
</body>
</html>
private void AddLstItems(ListBox lstValue, ListBox lstValueSelected, string strValue, string strText)
{
string[] arrValue = new string[] { };
string[] arrText = new string[] { };
arrValue = strValue.Trim().Split(';');
arrText = strText.Trim().Split(';');
lstValueSelected.Items.Clear();
for (int i = 0; i < arrValue.Length; i++)
{
ListItem newItem = new ListItem();
newItem.Value = arrValue[i].Trim();
newItem.Text = arrText[i].Trim();
lstValueSelected.Items.Add(newItem);
for (int j = 0; j < lstValue.Items.Count; j++)
{
if (arrText[i].Trim() == lstValue.Items[j].Text.Trim())
{
lstValue.Items.RemoveAt(j);
j--;
break;
}
}
}
}
/// 从一个列表里移到另一个列表里(一项或多项)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btndownMove_Click(object sender, EventArgs e)
{
if (LbUp.SelectedIndex == -1)
{
Response.Write("<script>window.alert('请选择要移动的项')</script>");
}
while (LbUp.SelectedIndex != -1)
{
ListItem listItem = new ListItem(LbUp.SelectedItem.ToString(), LbUp.SelectedValue.ToString());
LbUp.Items[LbUp.SelectedIndex].Selected = false;
if (LbDown.Items.FindByValue(listItem.Value) == null)
{
LbDown.Items.Add(listItem);
}
LbUp.Items.Remove(listItem);
}
LbDown.SelectedIndex = -1; if (LbDown.Items.Count > 0)
{
LbDown.Items[LbDown.Items.Count - 1].Selected = true;
}
}
lbDown 就是ListBoxB
==========
你错了,应该先移出(Remove),使其不属于任意一个 ListBox,然后将它添加到 另一个 ListBox,逻辑上,你想想,怎么可以有一个 ListItem 同时属于两个 ListBox 呢?当然,这里 ListBox 内部实际上没有做这项检验工作,
即使你将一个 ListItem 同时加到两个LixtBox 它也一样工作
看看行不?
private void Button1_Click(object sender, System.EventArgs e)
{
if(ListBox1.SelectedItem!=null)
{
ListBox2.Items.Add(ListBox1.SelectedValue);
ListBox1.Items.Remove(ListBox1.SelectedValue);
}
} private void Button2_Click(object sender, System.EventArgs e)
{
if(ListBox2.SelectedItem!=null)
{
ListBox1.Items.Add(ListBox2.SelectedValue);
ListBox2.Items.Remove(ListBox2.SelectedValue);
}
}