Confirm Delete in DataGrid

A

Amelyan

I found an example (below) in www.codeproject.com showing how to enable
DataGrid's delete buttons with confirmation. However, the example didn't
explain why the complicated if-condition is the way it is.

I tried just having the one line of code that adds confirmation script
without if-condition. However, this didn't work properly. DataGrid would
delete items even though I clicked Cancel in confirmation dialog.

Could anyone explain why the if-condition should be the way it is below in
order for the confirmation to work properly in DataGrid?

Thanks,
Amelyan

//In the DataGrid onItemDataBound method, add the following:

if ( e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.SelectedItem )
{
e.Item.Cells[0].Attributes.Add( "onClick", "return confirm('Are you
sure you wish to delete this record?');");
}
//* Note the magic number [0] should be the column that the button is
located.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top