Objective: This article provides step by step instruction for how to add delete confirmation to a GridView in ASP.NET.
Background: In a previous article I have illustrated a simple way of adding a client side confirm popup action to any button on your page. In this article we will use the same technique for the GridView delete button. It is a very simple procedure with the only tricky part being for the beginner is to learn how to expose this part of the GridView so we can assign a few simple properties to it. To read my previous tutorial please click ** Here **.
Procedure: Follow instructions below to accomplish this task. This article uses Visual Web Developer Express 2008, SQL Server 2005 Express and the NorthWind database.
- Let’s start by adding a new web form to your site and name it anything you want.
- From the Data tab of your toolbox, drag and drop a a GridView onto the page.
- If its not already open, click to open the GridView’s Smart Tag and add a new DataSource.
- For simplicity’s sake, let’s create a simple SqlDataSource of your choosing. For this article I’m using the NorthWind database from Microsoft.
- From the Employees table, I select the following columns: EmployeeID, LastName, and FirstName.
- Then I click on the “Advanced” button and check the “Generate Insert, Update and Delete Statements” checkbox and also the (Use optimistic concurrency” checkbox and click OK.
- Then I click Next and Finish to finish creating my GridView.
- Now from the GridView’s Smart Tag I check the boxes for Enable Paging, Enable Editing, and Enable Deleting. Really, the Enable Deleting is the only one we need, I’m just playing along to make things a bit more realistic.
- Now from the GridView’s Smart Tag and click on the Edit Column link.
- Then from the Selected Fields area I click to highlight the CommandField on top of the list to expose its CommandField properties on the right.
- From the ButtonType property I then select the Button instead of the Link display type and click the “convert this field into a template field” link and finally click OK.
- Now that I have converted this field into a template field and exposed its properties, I can go into code view an add a couple of properties to it. So, let’s go into code view and find the Delete button. It should look like below. Showing both the Edit and Delete button between the ItemTemplate tags:
- Finally, I add the following client side javascript property to my Delete button: OnClientClick=”return confirm(‘Are you sure?’);”. The result should look like the code below:
- Change the message for the popup dialogue as desired. Load the page into your browser to give it a spin.
- That’s it! Enjoy.

No comments yet
Comments feed for this article