引用删除按钮

本文关键字:按钮 删除 引用 | 更新日期: 2023-09-27 18:24:57

当编辑和删除按钮都位于GridView的第一列并且GridView的AutoGenerateDeleteButton属性为true时,如何引用GridView中的删除链接按钮。我指的是:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[0].Controls[0]).Attributes["onclick"] = "if(!confirm('Are you sure to delete this row?'))return   false;";
            }
        }
    }

代码运行良好,但当我在GridView1中删除一条记录时,它不会确认它并删除该记录。知道我哪里可能出错了吗?

引用删除按钮

我在我的系统中尝试过,当我尝试调试和查看e.Row.Cells[0]。控件时,我发现delete按钮存在于索引2处,所以你可以在那里附加delete js验证。

((LinkButton)e.Row.Cells[0].Controls[2]).Attributes["onclick"] = "if(!confirm('Are you sure to delete this row?'))return   false;";

一个建议,您也可以添加此检查,而不是RowState。

     if(e.Row.RowType == DataControlRowType.DataRow)
                {
 }