Thursday, September 13, 2007

How To Add Confirmation Box Before Deleting Data in DataGrid

You can add confirmation box before deleting data in your datagrid. Use the ItemCreated event of Datagrid. This event is raised when an item in the DataGrid control is created, both during round-trips and at the time data is bound to the control. See below example for your reference.


private void Datagrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.AlternatingItem e.Item.ItemType == ListItemType.Item)
{
LinkButton lb = (LinkButton)e.Item.Cells[0].Controls[0];
lb.Attributes.Add("Onclick","return confirm('Are you sure you want to delete this item?');");
}
}

No comments: