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

WordPress .htaccess Tips and Tricks

.htaccess files (or “distributed configuration files”) provide a way to make configuration changes on your Wordpress site on a per-directory basis. A .htaccess file, containing one or more configuration directives, is placed in a particular document directory on your Wordpress site, and the directives apply to that directory, and all subdirectories thereof. Using directives in .htaccess, you can block spam, secure your website, and control other website actions.

Passwordless logins in Unix

Connecting via SSH, requires a user provide his identity to the remote machine using one of several methods. One method lets you access the remote system without entering a password at each login. Normally, when you log in to a system, you authenticate by entering your password for that system. Your password goes, as it is typed, to the remote system, which authenticates it against the /etc/passwd or /etc/shadow file. By contrast, SSH allow a “password-less” authentication method based on public-key cryptography.

World of Warcraft – Level 19 Rogue Twink Notes

Back in the day, I build a level 19 Rogue twink that ruled in the World of Warcraft battlegrounds. Building the twink character was probably as hard or harder than leveling my Hunter to 80. Here are the notes that I gathered and used to level the twink to 19 (with the assistance of my Level 50 Druid and Level 80 Hunter).