将项目从一个 CheckBoxList 添加到另一个 CheckBoxList 如何使用 asp.net 检查该项目是否已存在于第二个 CheckBoxList 中C#
我编写的用于在 CheckBoxLists 之间移动的代码是:
protected void add_Click(object sender, EventArgs e)
{
for (int i = 0; i <= CheckBoxList2.Items.Count - 1; i++)
{
if (CheckBoxList2.Items[i].Selected)
{
CheckBoxList3.Items.Add(CheckBoxList2.Items[i]);
CheckBoxList3.ClearSelection();
}
}
}
我正在使用 add_Click 按钮在列表之间移动项目。 我需要在添加项目时进行验证。
请帮忙。谢谢..!!
请您参考如下方法:
引自 another answer通过@Joel-Coehoorn:
You need a using directive for System.Linq. .Where() is an extension method on IEnumerable (which IList implements) that is defined in the System.Linq namespace.
先取所有选中的元素
var checkedItems = CheckBoxList2.Items.Where(i => i.Selected);
然后遍历。
foreach(var item in checkedItems)
{
if(!CheckBoxList3.Items.Contains(item))
{
CheckBoxList3.Items.Add(item)
}
}
应该没问题。
