2.20. Adding a Delete Confirmation Pop-Up

Problem

You want to add to a GridView row a confirmation pop-up that appears whenever a user tries to delete a row in the GridView.

Solution

Add a Select button to each row of the GridView and a Delete button below the GridView. Whenever the Delete button is clicked, execute some client-side script that displays the confirmation pop-up, followed by some server-side code that performs the actual deletion.

In the .aspx file:

  1. Create an extra button column in the GridView to display a Select button.

  2. Add a Delete button below the GridView.

In the code-behind class for the page, use the .NET language of your choice to:

  1. Register the client-side script to be executed when the Delete button is clicked.

  2. Add an attribute to the Delete button that calls the delete script when the Delete button is clicked.

Figure 2-22 shows a GridView with this solution implemented. Examples 2-54, 2-55 through 2-56 show the .aspx and code-behind files for the application that produces this result.

Confirmation pop-up before deletion in a GridView output

Figure 2-22. Confirmation pop-up before deletion in a GridView output

Discussion

To display a confirmation pop-up when a user attempts to delete a row in a data table, you create a GridView in the same way you have done throughout this chapter, except that you add a button column to allow for row selection. Setting the ButtonType to "Link" outputs a hyperlink for selecting the row. (The ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.