Write javascript for confirm delete in gridview rowdeleting event

前端 未结 3 1223
傲寒
傲寒 2021-01-23 07:07

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

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 07:27

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

提交回复
热议问题