Friday, September 12, 2008

GridView - Confirm MessageBox before Deleting


public static void AddConfirmDelete(GridView gv, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (DataControlField dcf in gv.Columns)
{
CommandField cf = dcf as CommandField;
if (cf != null)
{
TableCell cell = e.Row.Cells[gv.Columns.IndexOf(dcf)];
foreach (Control ctl in cell.Controls)
{
switch (cf.ButtonType)
{
case ButtonType.Button:
if (ctl is Button && ((Button)ctl).Text == cf.DeleteText)
{
Button btn = (Button)ctl;
//btn.OnClientClick = "return confirm(\"Are you sure?\")";
btn.Attributes.Add("onclick", "return confirm(\"Are you sure Button?\")");
}


if (ctl is Button && ((Button)ctl).Text == cf.EditText)
{
Button btn = (Button)ctl;
//btn.OnClientClick = "return confirm(\"Are you sure?\")";
btn.Attributes.Add("onclick", "return confirm(\"Are you sure EditText?\")");
}
break;
case ButtonType.Image:
if (ctl is ImageButton && ((ImageButton)ctl).ImageUrl == cf.DeleteImageUrl)
{
ImageButton ib = (ImageButton)ctl;
//ib.OnClientClick = "return confirm(\"Are you sure?\")";
ib.Attributes.Add("onclick", "return confirm(\"Are you sure Image?\")");
}

if (ctl is Button && ((Button)ctl).Text == cf.EditText)
{
Button btn = (Button)ctl;
//btn.OnClientClick = "return confirm(\"Are you sure?\")";
btn.Attributes.Add("onclick", "return confirm(\"Are you sure EditImage?\")");
}
break;
case ButtonType.Link:
if (ctl is LinkButton && ((LinkButton)ctl).Text == cf.DeleteText)
{
LinkButton lb = (LinkButton)ctl;
lb.Attributes.Add("onclick", "return confirm(\"Are you sure Link?\")");
}

if (ctl is LinkButton && ((LinkButton)ctl).Text == cf.EditText)
{
LinkButton lb = (LinkButton)ctl;
lb.Attributes.Add("onclick", "return confirm(\"Are you sure EditLink?\")");
}
break;
}

}
}
}
}
}

No comments: