Question

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?');";

    }
}
Was it helpful?

Solution

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?');";
            }
    }
}

OTHER TIPS

Try using FindControl:

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

Good luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top