ListBox传值的问题,进来看看吧
/// <summary>
/// 添加
/// </summary>
protected void addBtn_Click(object sender, EventArgs e)
{
if (lstSelPro.SelectedItem != null)
{
if (lstSeledPro.Items.Count>0)
{
foreach (ListItem item in lstSeledPro.Items)
{
if (item.Text != lstSelPro.SelectedItem.Text)
{
lstSeledPro.Items.Add(lstSelPro.SelectedItem);
lstSelPro.Items.Remove(lstSelPro.SelectedItem);
lstSeledPro.SelectedItem.Selected = false;
}
}
}
else
{
lstSeledPro.Items.Add(lstSelPro.SelectedItem);
lstSelPro.Items.Remove(lstSelPro.SelectedItem);
lstSeledPro.SelectedItem.Selected = false;
}
}
}
报错:
错误: Sys.WebForms.PageRequestManagerServerErrorException: 集合已修改;可能无法执行枚举操作。
就是从list1 通过Button传到 list2 有重复就不添加 list button null
[解决办法]
listBox1.SelectedItems.OfType<ListItem>().ToList().ForEach(t => { listBox1.Items.Remove(t); listBox2.Items.Add(t); });
[解决办法]
//添加
protected void Button_Mritght_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.ListBox1.Items.Count; i++)
{
if ((this.ListBox1.Items[i].Selected) && (this.ListBox2.Items.FindByValue(this.ListBox1.Items[i].Value) == null))
{
ListItem item = this.ListBox1.Items[i];
this.ListBox2.Items.Add(item);
}
}
}
移除
protected void Button_Mleft_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.ListBox2.Items.Count; i++)
{
if (this.ListBox2.Items[i].Selected)
{
ListItem item = this.ListBox2.Items[i];
this.ListBox2.Items.Remove(item);
}
}
}