如果多次选中复选框 C# 如何发出警报

How to Alert if multiple checked in check box C#

我正在制作一个看板,现在我想在选中复选框时编辑项目。

但是,如您所知,复选框可以选中任何复选框。

在我的规则中,我只想编辑一项。

所以我想在多选复选框时显示警告消息。

我该怎么做?我不想禁用它。

因为删除项目时我使用了多选复选框。

我的代码如下。

protected void lnkbtnEdit_Click(object sender, EventArgs e)
{
    string editPageUrl = string.Empty;
    foreach (GridViewRow gRow in grvList.Rows)
    {
       CheckBox chkbox = (CheckBox)gRow.FindControl("chk");

       if (chkbox.Checked)
       {
          int id = Convert.ToInt32(gRow.Cells[1].Text);
                    editPageUrl = "http://ycchoi/sites/dev/_layouts/15/ListItemControl/EditListItem.aspx?ID=" + id;
          Response.Redirect(editPageUrl);
        }
        else
        {
           string message = @"
              <script type='text/javascript'>
                        alert('Please select item to Edit');
              </script>";
            ClientScript.RegisterClientScriptBlock(GetType(), "script", message);
                }
            }
        }

我会在 foreach 循环之外保留一个变量,存储被检查的行的 ID,然后查看该列表的长度以查看它是否等于 1,然后再允许用户对其进行编辑。这就是我在类似界面中处理它的方式(用户可以 delete/hide/highlight 多件事,但一次只能回复一个。)

List<int> ids = new List<int>();
foreach (GridViewRow gRow in grvList.Rows)
{
   CheckBox chkbox = (CheckBox)gRow.FindControl("chk");

   if (chkbox.Checked)
   {
      int id = Convert.ToInt32(gRow.Cells[1].Text);
                editPageUrl = "http://ycchoi/sites/dev/_layouts/15/ListItemControl/EditListItem.aspx?ID=" + id;
      ids.Add(id);
    }
}

if (ids.Count == 1)
{
    // do something with ids[0]
}
else
{
    // show error
}