为GridView-Delete列添加确认对话框(2种方法)
方法一:
给Delete列添加确认对话框最简单的方法是把CommandField的DeleteText属性设置为:
<div onclick="JavaScript:return confirm('Are you sure to delete?')">Delete</div>
加入div后会产生换行效果,若不想要换行效果可以使用span:
<span onclick="JavaScript:return confirm('Are you sure to delete?')">Delete</span>
方法二:
这种方法窃记住要判断状态,否则在有编辑按钮的时候找不到删除按钮的
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:"" + e.Row.Cells[1].Text + ""吗?')");
}
}
}
{
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:"" + e.Row.Cells[1].Text + ""吗?')");
}
}
}