Books for asp.net

Friday, August 31, 2007

Client side Delete Confirmation in a Gridview

This code use for show a message box when delete button clicked on gridview and when to prevent the accidental deletions.

protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (DataControlField dcf in gv1.Columns)
{
if (dcf.ToString() == "CommandField")
{
if (((CommandField)dcf).ShowDeleteButton == true)
{
e.Row.Cells[gv1.Columns.IndexOf(dcf)].Attributes
.Add("onclick", "return confirm(\"Are you sure?\")");
}
}
}
}
}