Javascript delete confirmation in Gridview or Repeater

A pretty common task needed within a gridview or repeated is to confirm an action when the user clicks on this. (typically a delete)

The solution to this is pretty simple.  Simple add the following code to the control tag.
OnClientClick='return confirm("Are you want to delete this ?");'

An example template field in a Gridview would look something like this
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CommandArgument='<%# Eval("ItemId") %>' CommandName="DeleteItem" Text="Delete" runat="server" OnClientClick='return confirm("Are you want to delete this Item?");' />
</ItemTemplate>
</asp:TemplateField>

2 Responses to "Javascript delete confirmation in Gridview or Repeater"

  1. JJ

    Thanks for this, but how do I do this if I want to add the name of the item I want to delete?

  2. Jose

    Here is a method that creates a custom field. Great for resuse and simple…

    http://csaspnet.blogspot.com/2009/08/aspnet-gridview-confirm-delete-button.html

Leave a Reply