Add delete confirmation to asp.net ListView control

The ASP.Net ListView control is a godsend for developers.  Drop the control on the form, hook in a datasource, and you have complete, out-of-the-box view, edit, insert, and delete functionality.  One thing missing though is a delete confirmation if the user clicks the delete button.  The Delete button is right next to the Edit button so it’s fairly easy to mistakenly click it and with an accidental click, the record is permanently deleted with no prompt or warning issued to the user.

Adding a delete confirmation prompt is very simple to do.  Using the JavaScript confirm() function, we can add one line to the control definition and implement the delete confirmation dialog box.  Simply add a OnClientClick() call to the button control(s) in the ListView definitions (see the bold text in the example below).

<asp:Button ID=”DeleteButton” runat=”server” CommandName=”Delete” Text=”Delete” OnClientClick=”return confirm(‘Are you certain you want to delete?’);”/>

The result will be the pop of a delete confirmation dialog box as seen in the screenshot below.  Clicking the delete button pops the dialog.  If the user clicks the Cancel button then the dialog closes and no action is taken on the record.  Conversely, if the OK button is clicked then the dialog closes and the record deletion continues.

 

image

Leave a Reply

Related Post

Universal Studios California Backlot Tour

One of the most interesting things we saw on our 2001 Southern California vacation was the backlot tour of Universal Studios. Below are pictures I took during the tour of the Universal Studios California Backlot Tour. Tours are given in trams with a guide narrating the scenes. Depending on when you go, areas of the backlot are sometimes closed while they are filming.

Useful Unix Commands

Useful Unix commands To search text in files and all subdirectories find [directory] -name [files] | xargs grep [text to search for] Example to find “insert” in the odb directory (search all files in that directory): find odb -name “*” | xargs grep “insert” Run job in batch now at -s now < thejob.sh List […]

Reading the engine diagnostic codes on a Nissan Altima

How to read the engine diagnostic code The following applied to a 2004 Nissan Altima but I believe any Nissan uses this method. The Service Engine Soon light will come on and stay on when the onboard computer detects a problem with your car.  When a problem arises, a unique engine diagnostic code or codes […]