ASP.Net GridView 删除提醒

在绑定好数据后,添加了CommandField的删除列,然后双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:

其中Cells[3]中的3是你的删除链接所在列号,第一列号为0依次类推

    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[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
            }
        }

    }

然后双击GridView的RowDeleting事件,添加如下代码,其中BLL.BSpeClass.delete(id)为你的后台操作,通过数据的主键值删除一行数据

 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //string id = GridView1.DataKeys[e.RowIndex].Value.ToString();如果你要用此条命令获得主键值,一定要设置GridView的DataKeysNames为向相应主键值如class_id
            string id = GridView1.Rows[e.RowIndex].Cells[0].Text;
            if (BLL.BSpeClass.delete(id) == true)
            {
                Response.Write("<script>alert('删除成功!')</script>");
                BLL.BFile.go("SearchSpecClass.aspx");
            }
            else
            {
                Response.Write("<script>alert('删除失败!')</script>");
                BLL.BFile.go("SearchSpecClass.aspx");
            }
        }

 

感谢以下博主

http://www.baidu.com/p/hexiannian1987?from=zhidao

http://www.pengzhiyong.com/blog/6.html  //GridView的DataKeyNames、DataKeys、Rows、Cells、Controls的概念与用法。

你可能感兴趣的:(ASP.Net GridView 删除提醒)