简体   繁体   中英

How to Alert if multiple checked in check box C#

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

I'd keep a variable outside of the foreach loop, storing the ID of the rows that are checked, then look at the length of that list to see if it's equal to 1 before allowing the user to edit it. This is how I approached it in a similar interface (where a user can delete/hide/highlight multiple things, but only reply to one at a time.)

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM