0

I'm running into an issue having a "delete confirmation" pop up if someone selects the "Delete" link in a gridview.

More specifically, the pop up does work when clicking the "Delete" link, but the pop-up also comes up if I click the "Edit" link next to it in the same cell, and then click the "Cancel" button for the update operation when it gives the options of "Update" and "Cancel".

I believe it's because I am accessing the Delete control by index, and when I click the Edit button, the Cancel button for the "Edit" link then takes the index of where the "Delete" button is by default. Obviously the pop-up for the "Cancel" operation is not desired. I'm using the built-in "Allow Editing" and "Allow Deleting" options for the gridview. Below is the code I'm using.

protected void actionPlanGirdView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) 
    {
        // reference the Delete LinkButton
        LinkButton db = (LinkButton)e.Row.Cells[0].Controls[2];

        db.OnClientClick = "return confirm('Are you certain you want to delete the record?');";

    }
}
sgeddes
  • 62,311
  • 6
  • 61
  • 83
kyle_13
  • 1,173
  • 6
  • 25
  • 47

2 Answers2

1

Update, I found a solution to this. In the situation where you are using the auto-generated Insert and Delete buttons for a .NET gridview, and you want to access the delete button programatically, I did so with the below, accessing the Text property of the LinkButton. The inner if statement checks to see if the LinkButton is the Delete link, as if you also use an auto-generated Insert link, that index position can be the place of the Cancel button for the Update/Cancel combo when you click Insert in the Gridview.

protected void actionPlanGirdView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // reference the Delete LinkButton
        LinkButton db = (LinkButton)e.Row.Cells[0].Controls[2];

            if (db.Text == "Delete")
            {
                db.OnClientClick = "return confirm('Are you certain you want to delete the record?');";
            }
    }
}
kyle_13
  • 1,173
  • 6
  • 25
  • 47
0

Try using FindControl:

LinkButton deleteButton = (LinkButton)e.Row.FindControl("deleteButton");

Good luck.

sgeddes
  • 62,311
  • 6
  • 61
  • 83
  • It didn't work. I think because I don't have an ID to give the delete button because it's auto-generated: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. – kyle_13 Feb 11 '13 at 19:41
  • @kyle_13 Where are you creating your Delete button? Can you post that code? – sgeddes Feb 11 '13 at 19:59
  • I'm not doing any programming to create the delete link, I just select the gridview in design view, and then choose the "Enable Deleting" under "Gridview tasks" when you select the arrow to configure things. This then produces.. ` ****` – kyle_13 Feb 11 '13 at 20:54
  • @kyle_13 -- Just saw your edits -- while I haven't tested this, you can try: List links = Tools.FindControlsRecursive(e.Row.Controls[0]); LinkButton delete = links.Single(c => c.Text == "Delete"); – sgeddes Feb 11 '13 at 21:05
  • @kyle_13 -- well, that's not all -- see this post: http://forums.asp.net/t/1255553.aspx – sgeddes Feb 11 '13 at 21:06
  • @kyle_13 -- btw, I'm just not use to using the auto generated buttons -- my preference is to create these buttons manually -- much easier to control in those cases. -- this way you add OnClientClick to the button directly and not have to worry about the backend code. Best of luck. – sgeddes Feb 11 '13 at 21:09