Click to See Complete Forum and Search --> : confirmation from user from a delete button in the DataGrid


sansircar
January 25th, 2006, 09:22 AM
One of the columns of the datagrid is a TemplateColumn containing a Delete button.
i'm deleting a row on the click of the Delete Button.
How do i put in a confirmation - "Are you sure ..."
for the user before, the row gets deleted. Since the delete code is a part of the following event -

private void grdPayments_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
}
thus cannot even attach an "onClick" attribute to it, had it been a normal button.
please advise.

mmetzger
January 25th, 2006, 10:41 AM
You have to catch the OnItemDataBound event and add a handler to the button at that point in time.

This will vary based on your needs, but:


void MyDataGrid_OnDataBound(Object sender, DataGridItemEventArgs e)
{
if ((e.Item.ItemType.ToString() != "Header") && (e.Item.ItemType.ToString() != "Footer"))
{
if (ShowLinks.Text == "true")
{
ImageButton DeleteButton = e.Item.FindControl("DeleteMyDataLink") as ImageButton;
string MaintenanceID = DeleteButton.CommandArgument;
DeleteButton.Attributes.Add("OnClick","return confirm('Are you sure you want to Delete Record " + MaintenanceID + "?');");
DeleteButton.Visible = true;
}
}
}