在GridView的删除按钮上增加客户端确认提示,其实很简单,原理就是在GridView进行数据绑定的时候,每绑定一行就找到本行上的删除按钮,然后为此按钮增加客户端事件onclick,在此事件中弹出提示窗口即可。
GridView的事件是RowDataBound。
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = (LinkButton)e.Row.Cells[ 3 ].Controls[ 0 ];
lb.Attributes.Add( " onclick " , " Javascript:return confirm('真的删除- " + e.Row.Cells[ 1 ].Text + " -吗?') " );
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = (LinkButton)e.Row.Cells[ 3 ].Controls[ 0 ];
lb.Attributes.Add( " onclick " , " Javascript:return confirm('真的删除- " + e.Row.Cells[ 1 ].Text + " -吗?') " );
}
}
一个GridView的单元中可能会有多个Control,所以要在指定的单元格上使用Controls[0]来获取按钮控件。