How to Alert if multiple checked in check box C#

时间:2017-01-10 02:29:42

标签: javascript c# gridview checkbox alert

I`m making a board and now I want to edit item when check box checked.

But, as you know check box can check any check box.

In my rule I want to edit just one item.

So I want to display alert message when multiple selection of checkbox.

How can I do that?? I don`t want to disable it.

Because when delete item I`m using multiple selection of checkbox.

My code is below.

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);
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

我将一个变量保存在foreach循环之外,存储已检查行的ID,然后在允许用户之前查看该列表的长度以查看它是否等于1编辑它。这就是我在类似界面中接近它的方式(用户可以删除/隐藏/突出显示多个内容,但一次只回复一个。)

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
}