How can I get a delete confirm in asp:GridView

asp.net

Solution

If you have a delete button for each row in a GridView, here is what I think is the best (and definitely easiest) way to have it put up an alert asking for confirmation. Here is the full aspx code (there is no code-behind needed):

<asp:TemplateField ShowHeader="False">
  <ItemTemplate>
  <asp:LinkButton ID="DeleteButton" runat="server"
      CausesValidation="False"
      CommandName="Delete"
      OnClientClick='<%# Eval("Title", "return confirm(\"Delete the datasource {0}?\");") %>'
      Text="delete" />
  </ItemTemplate>
</asp:TemplateField>

A couple of notes:

- The CommandName must be Delete - that is what maps this to the GridView delete functionality.

- Title is the data column name of the title of that row. What you use here will depend on the column/property names of your data.

- Same for the word datasource - that is what we have rows of. You need to replace with what your data is called.

- I think <%$ resource_name %> should work for the text - but have not done that yet.

Problem

How do I pop up a delete confirm for a delete button in an asp:GridView?

Original source