Adding a confirmation popup for delete buttons defined within templates

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as