IF you are allowing users to delete rows from a datagrid, you may want to give the the life to confirm the delete first.
By: John Kilgo Date: July 17, 2003 Download The Code.
Printer Friendly Version
Allowing a user to delete a row from a database table using a Datagrid is handy, but dangerous if you just delete immediately upon pressing a button. A better way is to pop up a dialog asking the user to confirm his or her desire to delete the record. to do this requires a little javascript, as well as a change in the usual way of placing a delete button (linkbutton or pushbutton) in the datagrid. Following the VS.NET way, a ButtonColumn gets added to the datagrid. The ButtonColumn , HAS No ID Property and we need one in Order To Associate The JavaScript Function with the button. We can get around this by adding a template column with a button rather Than Adding a ButtonColumn.
WE Add The Template Column As Shown Below In The File, Confirmdeldg.aspx. There Are Se .aspx File. We'll Take THEM IN ORDER OF APPEARANCE. First, Between the
... head> tags is our javascript function named confirm_delete. This simply pops up a confirmation dialog asking the user if he is sure he wants to delete the record. If OK is clicked, the delete happens. If Cancel is clicked, nothing happens. The second thing to note is that our first BoundColumn is an invisible column containing the ProductID (we are using the Northwind Products table), which is the primary key we will use for the delete. Most importantly, please note that we have added a Template Column in which we have placed an asp: Button (you could use a LinkButton instead if you prefer) We have given it an ID of "btnDelete" and a CommandName of "Delete" The latter is what makes it work with the Datagrid's.. OndeleteCommand.<% @ Page language = "vb" src = "confirmdeldg.aspx.vb" inherits = "confirmdeldg" autoeventwireup = "false"%>