Adding a confirmation popup for delete buttons defined within templates

The DataList and DataGrid controls easily allow to add a “Delete” button/hyperlink in your template. When clicked, this button/link raises a DeleteCommand event that you can handle to delete the data item of the parent DataList’s/DataGrid’s row. Here’s an example that shows how to declare two column in a DataGrid, that creates one a delete link, and the other a delete push button:


The in the code-behind file you should handle the control’s DeleteCommand event. While this simple implementation surely works fine, it is not the best result you can get – if the user erroneously clicks the delete button/link, the correspondent data item will be deleted immediately, without an explicit confirmation by the user. Adding a confirmation popup dialog is not a big work however, and it greatly improves the usability. What we have to do is adding some javascript in response of the client-side onClick event. This is done by adding an entry in the control’s Attributes collection, when the DataGrid’s row is created. The code and its comments below explain in detail what is done:

Private Sub DataGrid1_ItemCreated(ByVal sender As Object, _    ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles _    DataGrid1.ItemCreated    ' process data rows only (skip the header, footer etc.)    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = _        ListItemType.AlternatingItem Then        ' get a reference to the LinkButton of this row,        '  and add the javascript confirmation        Dim lnkDelete As LinkButton = CType(e.Item.Cells(3).Controls(0), _            LinkButton)        lnkDelete.Attributes.Add("onclick", _            "return confirm('Are you sure you want to delete this record?');")                ' get a reference to the Button of this row,        '  and add the javascript confirmation        Dim btnDelete As Button = CType(e.Item.Cells(4).Controls(0), Button)        btnDelete.Attributes.Add("onclick", _            "return confirm('Are you sure you want to delete this record?');")    End IfEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.