【问题标题】:Deleting multiple checked items from an SQL bound checklist从 SQL 绑定清单中删除多个选中项
【发布时间】:2018-04-11 16:03:32
【问题描述】:

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

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

【问题讨论】:

    标签: c# asp.net database ado.net checklistbox


    【解决方案1】:

    您的问题是您使用的是 Selected 项目,而不是 Checked 项目。这是两个不同的东西(选中的是蓝色突出显示,选中的是复选框)。在您的情况下使用Checked 属性,一切都应该正常工作。

    旁注,您还可以在您的foreach 中使用ListViewCheckedItems 属性来简化您的代码。

    foreach (ListItem Item in FoodChecklist.CheckedItems)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多