0

How to refer to delete link button in a GridView when both edit and delete buttons are located in the first column of the GridView and AutoGenerateDeleteButton property of GridView is true. I'm referring to it as:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[0].Controls[0]).Attributes["onclick"] = "if(!confirm('Are you sure to delete this row?'))return   false;";
            }
        }
    }

Code runs fine but when I delete a record in GridView1, it doesn't confirm it and deletes the record. Any idea where I might be going wrong?

3
  • WinForms or WPF? Please tag your question appropriately.
    – RenniePet
    Sep 27, 2014 at 16:11
  • Can you add the generated HTML of just one row?
    – ekad
    Sep 27, 2014 at 17:23
  • @RenniePet WinForms. Sep 30, 2014 at 8:45

1 Answer 1

0

I have tried in my system, when i tried to debug and look into e.Row.Cells[0].Controls, i found that delete button exist at index 2, so you can attach delete js validation there.

((LinkButton)e.Row.Cells[0].Controls[2]).Attributes["onclick"] = "if(!confirm('Are you sure to delete this row?'))return   false;";

One suggestion, you can also add this check , instead of RowState.

     if(e.Row.RowType == DataControlRowType.DataRow)
                {
 }
2
  • Your code works locally but after publishing it to server, its not working on server. Any idea why? Sep 30, 2014 at 8:44
  • can you debug there, i mean just put response.write( ((LinkButton)e.Row.Cells[0].Controls[2]).Text) and respons.end, to see, what you are getting there! Sep 30, 2014 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.