[英]Deleting multiple checked items from an SQL bound checklist
So right now, I have tried the following code, however it only removes one list item. 因此,现在,我尝试了以下代码,但是它仅删除了一个列表项。 It seems that after it deletes the first item, the list refreshes and the other checked item does not get deleted. 似乎在删除第一个项目之后,列表将刷新,而其他选中的项目不会被删除。 How can I go around this so that all the checked items are deleted from the DB Table? 我该如何解决,以便将所有选中的项目从数据库表中删除?
//NewFoodInputTextBox is an ASP.NET TextBox which takes input
//just fine and outputs status/error updates as well.
protected void DeleteFoodButton_Click(object sender, EventArgs e)
{
try
{
int delCount = 0;
int prevDelCount = 0;
string status = "";
foreach (ListItem Item in FoodChecklist.Items)
{
if (Item.Selected)
{
FoodList.Delete(); //only deletes 1 item
prevDelCount = delCount;
delCount += 1;
if (delCount > prevDelCount) //printing out the deleted items
{
status = status + " " + Item.ToString(); //returns all checked items normally
}
}
}
if (delCount == 0)
{
NewFoodInputTextBox.Text = "Nothing selected to delete";
}
else
{
NewFoodInputTextBox.Text = "Deleted the following: " + status;
}
}
catch
{
NewFoodInputTextBox.Text = "Unexpected behavior detected";
}
}
Your problem is that you are using the Selected
items, and not the Checked
ones. 您的问题是您使用的是“ Selected
项目”,而不是“已Checked
项目”。 These are 2 different things (selected is the blue highlight, checked is the checkbox). 这是2种不同的事物(选中的是蓝色突出显示,选中的是复选框)。 Use the Checked
property in your condition and everything should work fine. 在您的情况下使用Checked
属性,一切正常。
Side note, you could also use the CheckedItems
property of the ListView
in your foreach
to simplify your code. 旁注,您还可以在foreach
中使用ListView
的CheckedItems
属性来简化代码。
foreach (ListItem Item in FoodChecklist.CheckedItems)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.