many times I have seen this question posted in different forums. I, myself had asked it time ago and eventually found a solution.
Is there a way to add a confirmation message to a delete button of my datagrid?
There sure is. The javascript is pretty easy:
javascript: return confirm('Are you sure you want to delete this record?');
The problem is adding it to a button that is created on-the-fly. The best way to access this button is by casting it after it's created and before it's pressed, and the only place where we can do this is in the ItemCreated Event:
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType lit = e.Item.ItemType;
if (lit == ListItemType.Item)
{
Button btnDelete = (Button) e.Item.FindControl("btnDelete");
btnDelete.Attributes.Add("OnClick", "javascript: return confirm('Are you sure you want to delete this record?');");
}
}
et voila'
now we have a delete button that will ask for a confirmation before executing it's code.
I hope this will help most of you, and I hope this Faq finds you in great health.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.