Skip to content

Confirmation delete window – deleting record when click delete or cancel

I have a Javascript file that contains the function for calling the confirmation delete window, the code used is similar to the following:

function Delete() 
{
    confirm('Delete this user');
    
}

I have a gridview with a template field that can delete a row in the gridview, this is a snippet of what I have:

<asp:TemplateField HeaderText="Amend" ShowHeader="false">
               <ItemTemplate>
                   <asp:LinkButton ID="btnedit" runat="server" 
            CommandName="Edit" Text="Edit" ></asp:LinkButton>
               </ItemTemplate>
               <EditItemTemplate>
                   <asp:LinkButton ID="btnamend" runat="server" 
            CommandName="Amend" Text="Amend" ></asp:LinkButton>
            <asp:LinkButton ID="btndelete" runat="server" 
            CommandName="Delete" Text="Delete" OnClientClick="return Delete() "></asp:LinkButton>
               </EditItemTemplate>
            </asp:TemplateField>

Within the aspx.vb page I have code to do the deletion of the record (this works fine), I have this code within the gridview property OnRowDeleting="RowDeleting1"

Protected Sub RowDeleting1(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
' code to do the deletion

When the confirmation of the delete appears (options are to delete or cancel), the deletion occurs when I click delete or cancel. Ideally I only want the deletion to occur when I click delete and just stay on the page when I click cancel, would anyone be able to advise what I am doing wrong?

Answer

Replace confirm('Delete this user'); with return confirm('Delete this user');