自定义带提示的GridView删除按钮

大家都知道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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值