C# – Delete Confirmation Message in CommandField

asp.netcgridview

I am trying get confirmation message while click on delete button in GridView. If I conform only the row will be delete in GridView.

*.ASPX

<Columns>

    <asp:CommandField ButtonType="Button" ShowDeleteButton="true" />

</Columns>

*.ASPX.CS

protected void grdPersTable_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Button buttonCommandField = e.Row.Cells[0].Controls[0] as Button;
        buttonCommandField.Attributes["onClick"] = 
               string.Format("return confirm('Are you want delete ')");
    }
}

protected void grdPersTable_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    Label lbl0 = (Label)grdPersTable.Rows[e.RowIndex].FindControl("lblId");
    txtId.Text = lbl0.Text;
    obj.DeleteV(Convert.ToInt32(txtId.Text));
    grdPersTable.DataSource = obj.GetTableValues();
    grdPersTable.DataBind();        
    lblMessage.Text = "Deleted successfully !";
}

Best Answer

I got answer friends

<asp:TemplateField>
      <ItemTemplate>
            <asp:Button ID="deletebtn" runat="server" CommandName="Delete" 
             Text="Delete" OnClientClick="return confirm('Are you sure?');" />
      </ItemTemplate>
</asp:TemplateField>

i changed CommandField to TemplateField

Thanks !

Related Topic