Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a delete confirm in asp:GridView

Tags:

asp.net

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

like image 557
David Thielen Avatar asked Dec 06 '25 13:12

David Thielen


1 Answers

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:

  1. The CommandName must be Delete - that is what maps this to the GridView delete functionality.
  2. 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.
  3. Same for the word datasource - that is what we have rows of. You need to replace with what your data is called.
  4. I think <%$ resource_name %> should work for the text - but have not done that yet.
like image 84
David Thielen Avatar answered Dec 08 '25 06:12

David Thielen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!