Write javascript for confirm delete in gridview rowdeleting event

I am deleting a row from gridview for which i have used gridview’s default delete command field. On clicking, the gridview row deleting command gets fired and the selected row gets deleted. All good till now.

But before the row gets deleted, i need to set confirmation message to user. On clicking of OK button the row should get deleted else not (on click of cancel button).

I have the code as;

return confirm('Are you sure to delete?');

But this works fine if there is a linkbutton (instead of command field) as i could easily write on OnClick event of linkbutton and could add the attributes in Gridview RowDataBound event.

How the same would work for command’s field delete button? Please guide!

Thanks!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This article explains how to do exactly what you need:

http://www.codeproject.com/KB/webforms/Gridview_Delete_confirmLS.aspx

And here is the code you need to do it:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // loop all data rows
        foreach (DataControlFieldCell cell in e.Row.Cells)
        {
           // check all cells in one row
           foreach (Control control in cell.Controls)
           {
                // Must use LinkButton here instead of ImageButton
                // if you are having Links (not images) as the command button.
                ImageButton button = control as ImageButton;

                if (button != null && button.CommandName == "Delete")
                    // Add delete confirmation
                    button.OnClientClick = "return confirm('Are you sure you want to delete this record?');";
            }
        }
    }
}

Method 2

Here is code which you can use….

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        foreach (DataControlField dcf in GridView1.Columns)
        {

            if (dcf.ToString() == "CommandField")
            {
                if (((CommandField)dcf).ShowDeleteButton == true)
                {
                    e.Row.Cells[GridView1.Columns.IndexOf(dcf)].Attributes.Add("onclick", "return confirm('Are you sure you want to delete?');");
                }
            }
        }
    }
}

Method 3

Try this.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        foreach (DataControlFieldCell cell in e.Row.Cells)
        {
            foreach (Control control in cell.Controls)
            {
                // Choose between Button, ImageButton and LinkButton.
                // as my ButtonType="Button". 
                Button button = control as Button;
                if (button != null && button.CommandName == "Delete")
                {
                    string script = "if(confirm('Are you sure to delete?')) __doPostBack('{0}','{1}${2}'); else return false;";
                    string clickEvent = String.Format(
                        script,
                        GridView1.ClientID,
                        button.CommandName,
                        button.CommandArgument);
                    button.Attributes.Add("onclick", clickEvent);                                 
                    break;
                }
            }
        }
    }
}

Much more dirty than I originally anticipated. Better to use a asp:TemplateField 🙂

Please note that my ButtonType="Button"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x