Click here to Skip to main content
12,826,440 members (23,846 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 13 May 2009

Confirm Before Deleting a Grid Item

, 13 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
When deleting an item from an ASP.NET grid, it would be nice to ask the user to confirm this is what they really meant to do.

When deleting an item from an ASP.NET grid, it would be nice to ask the user to confirm this is what they really meant to do. After all, it is very easy to mouse click somewhere by accident. And, what would even be nicer is if this confirmation takes place on the client (browser) instead of requiring yet another round trip to the server.

Fortunately, this task is very easy to do. As you might expect, the answer is to use JavaScript. In some cases, setting up JavaScript in an ASP.NET page can get a little involved. However, a simple script can be added using the OnClientClick property, which is available with many ASP.NET controls.

Listing 1 shows part of the ASP.NET code for a GridView control. This code includes an ItemTemplate that defines a Delete button and includes some confirmation JavaScript in the OnClientClick property.

<asp:GridView ID="GridView1" runat="server" />
        <asp:LinkButton ID="lnkDelete" runat="server"
          CommandArgument='<%# Bind("ItemID") %>'
          OnClientClick="return confirm('Delete this item?');">
Listing 1: JavaScript to confirm deleting a GridView item

This simple JavaScript calls confirm(), which returns true if the user selects Yes. The code associated with posting back the form and deleting the item only executes if this script returns true.

So, that’s a very simple technique that is easy to implement and works very well. And, because it uses JavaScript, it doesn’t perform the postback to the server unless the user confirms they really do want to delete the grid item.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jonathan Wood
Black Belt Coder
United States United States
Jonathan Wood has been a software developer for more years than he cares to admit. His current focus is on using C# and ASP.NET MVC to develop website applications, and C++ and MFC to develop desktop applications. His consulting company, SoftCircuits, is known for producing various commercial and shareware products.

Having an entrepreneurial spirit, Jonathan also has a number of other online businesses that he built from scratch such as Black Belt Coder, Trail Calendar and others. He also has expertise in designing brands (including graphics and logos) and search-engine optimization (SEO).

Jonathan is always willing to discuss consulting work or joint ventures with people looking to develop software or online businesses.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Steven Berkovitz13-May-09 8:13
memberSteven Berkovitz13-May-09 8:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 13 May 2009
Article Copyright 2009 by Jonathan Wood
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid