上面写错了... 应该是 foreach (ListItem li in DropDownList1.Items) { if (li.Selected == true) { DropDownList1.Items.Remove(li); } } ============ 请问,你这段代码可以如期运行迈?悄悄告诉你, foreach 是只读遍历访问滴
解决了,原来只要小小转换一下思维 Dim n As Integer = CheckBoxList1.Items.Count For i As Integer = n - 1 To 0 Step -1 If CheckBoxList1.Items.Item(i).Selected = True Then CheckBoxList1.Items.RemoveAt(i) End If Next
原来你要 VB 的, 可是我觉得你上面的代码不得行,会出现数组越界问题,当 Remove 掉一个后,Count 就少了一个, 可是你的迭代起始值却是固定的,C# 是这样写的for (int i = 0; i < MyListBox.Items.Count; ) { ListItem item = MyListBox.Items[i]; if (item.Selected) { MyListBox.Items.Remove(item); continue; } i++; }
foreach(datalist或datagrid的每一行)
{
用findcortrol找到这行中的checkbox
判断是否选中
选中就将该行的ID或其他唯一标识存进一个数组
}
对最后得到的数组的没一项进行相应的删除操作
for(int i=0;i<this.CheckBoxList1.Items.Count;i++)
{
if(this.CheckBoxList1.Items[i].Selected)
{
cmd.CommandText="delete from GroupTable where GroupID="+this.CheckBoxList1.Items[i].Value.ToString();
cmd.ExecuteNonQuery();
}
}
{
if(lb.selected==true)}
ListItem item = MyListBox.Items[i];
if (item.Selected) {
MyListBox.Items.Remove(item);
continue;
}
i++;
}
应该是
foreach (ListItem li in DropDownList1.Items)
{
if (li.Selected == true)
{
DropDownList1.Items.Remove(li);
}
}
{
if(CheckBoxList.Items[i].Selected)//判断是否选中
{
//做你要进行的删除操作
}
}
写错了。不好意思```
{
if(CheckBoxList.Items[i].Selected)//判断是否选中
{
//做你要进行的删除操作
}
}
----------------
同意!另外:在删除的时候,用foreach循环是不对的,会出错,foreach只读!
应该是
foreach (ListItem li in DropDownList1.Items)
{
if (li.Selected == true)
{
DropDownList1.Items.Remove(li);
}
}
============
请问,你这段代码可以如期运行迈?悄悄告诉你, foreach 是只读遍历访问滴
Dim n As Integer = CheckBoxList1.Items.Count
For i As Integer = n - 1 To 0 Step -1
If CheckBoxList1.Items.Item(i).Selected = True Then
CheckBoxList1.Items.RemoveAt(i)
End If
Next
ListItem item = MyListBox.Items[i];
if (item.Selected) {
MyListBox.Items.Remove(item);
continue;
}
i++;
}