大家都知道GridView自带删除按钮在删除一条记录时不会有任何提示,那么改造后在删除GridView中的一条记录时弹出一个提示信息。
步骤如下:
1.在GridView中添加一个模板列,然后编辑模板,添加一个LinkButton。
2.在GridView_RowDataBound事件中添加代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
("LinkButton1")).Attributes.Add("onclick", "Javascript:return
confirm('这将彻底删除这条记录,你确定吗?');");
}
3.双击摸板中的LinkButton,在LinkButton_Click中添加删除代码:
protected void LinkButton1_Click(object sender, EventArgs e)
{
SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString);
myconn.Open();
string sql = "delete xxx where ID = " + ((LinkButton)sender).CommandArgument.ToString();
SqlCommand mycmd = new SqlCommand(sql, myconn);
mycmd.ExecuteNonQuery();
myconn.Close();
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();
}
转载于:https://www.cnblogs.com/nextsoft/articles/774058.html