Has there ever been a time when you wanted a popup message box to confirm before actually performing some action in a .NET
I had a
DataGrid full of employees, with a Delete button and a whole lot of “Click-Happy” users. I bet you know the type. ;) I wanted a confirmation message box to appear when the Delete button was pressed. If they press “Yes”, then I delete the employee. If they press “Cancel”, then don’t perform the delete method.
Here are the basic steps to take:
DataGrid to your aspx page:
<asp:datagrid id="myDataGrid" runat="server" width="512px">
<asp:buttoncolumn text="Delete" buttontype="PushButton"
private void setupClientScript()
string js = @"
return confirm('Are you sure you wish to delete this record?');
RegisterClientScriptBlock( "ConfirmDeletion", js );
Call the method you just created to get it registered for the page. I usually do it in the
onItemDataBound method, add the following:
if ( e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.SelectedItem )
e.Item.Cells.Attributes.Add( "onClick", "return ConfirmDeletion();" );
* Note the magic number  should be the column that the button is located.
Call your delete method. In your
Command method, add the following:
RemoveMe( int.Parse( e.Item.Cells.Text.ToString() ) );
* Note the magic number  is the column of my key value. I pass my key to the method
RemoveMe and perform my delete action there.
Download and look at the source code for a fully functional sample.