Jeremy McGee Jeremy McGee - 1 year ago 127
Javascript Question

Adding a JavaScript confirmation prompt to a delete command button in an ASP.NET grid view?

So I have an ASP.NET grid view:

<asp:GridView ID="gvReferences" runat="server" AutoGenerateColumns="False" ShowHeader="False"
OnRowDeleting="gvReferences_RowDeleting" onrowcreated="gvReferences_RowCreated">
<asp:TemplateField ItemStyle-Width="400px">
<asp:Label ID="lblId" Visible="false" runat="server" Text='<%# Eval("Id") %>' />
<asp:Label ID="lblAssociatedSpecies" runat="server" Text='<%# Eval("Text") %>' />
<asp:Label ID="lblKind" runat="server" Text='<%# Eval("Kind") %>' />
<asp:CommandField ButtonType="Button" DeleteText="delete" ShowDeleteButton="True"
ShowCancelButton="False" EditText="edit" ShowEditButton="True">
<ControlStyle Width="60px" />

I'd like to attach some JavaScript to the Delete command button to ask for confirmation before a row is deleted.

Any ideas?

Answer Source

You could always use a TemplateField rather than the CommandField.

    <asp:Button name="btnDelete" commandName="Delete" OnClientClick="return confirm('Delete 
this Item');" Text="Delete" runat="server" />
    <asp:Button name="btnEdit" commandName="Edit" Text="Edit" runat="server" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download