0

所以现在,我已经尝试了以下代码,但是它只删除了一个列表项。似乎在删除第一个项目后,列表会刷新,而另一个选中的项目不会被删除。我该如何解决这个问题,以便从数据库表中删除所有选中的项目?

//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";
    }
}
4

1 回答 1

0

您的问题是您正在使用这些Selected物品,而不是Checked那些物品。这是两个不同的东西(选中的是蓝色突出显示,选中的是复选框)。在您的条件下使用该Checked物业,一切都应该正常。

旁注,您还可以使用中的属性CheckedItems来简化代码。ListViewforeach

foreach (ListItem Item in FoodChecklist.CheckedItems)
于 2018-04-11T16:21:17.150 回答