I want to delete a record from a GridView. Before that, ask to confirm how “Are you sure you want to delete?”.

I want to delete a record from a GridView. Before that, ask to confirm how “Are you sure you want to delete?”

I used the command field in the GridView,

<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />

I wrote a function in javascript

function confirm_Delete()
{
    var r = confirm("Are you sure you want to Remove this Record!");

    if (r == true)
    {
        alert("Record Deleted");
        return true;
    }
    else 
    {
        return false;
    }
}

As I will call it when deleting a click. Please suggest!

+5
source share
4 answers

You cannot achieve this using the command field, you must create a template field:

<asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete" 
             OnClientClick="javascript:return confirm('Are you sure you want to Remove this Record!');">
            </asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>

He will behave the same way as currently with the team field.

+5
source

, @Muhammad, script "", :

public void MethodForDeletingARecord()
{
    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "RecordDeletedMessage", "javascript:alert('Record Deleted');", true);
}
+5

I think you can achieve this for the command field

Assuming this will be the first column, Found here

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

        db.OnClientClick = "javascript:return confirm('Are you certain you want to delete?');"
    }
}
+4
source
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "return Conformation();");
    }
}
+3
source

All Articles