GridView 删除数据

第一种直接在前台删除按钮上写代码

 <asp:TemplateField HeaderText="操作" ShowHeader="False">
             <EditItemTemplate>
                 <asp:LinkButton ID="lBtnUpdate" runat="server" CausesValidation="True"
                     CommandName="Update" Text="更新"></asp:LinkButton>
                 &nbsp;<asp:LinkButton ID="LBtnCancel" runat="server" CausesValidation="False"
                     CommandName="Cancel" Text="取消"></asp:LinkButton>
             </EditItemTemplate>
             <ItemTemplate>
                 <asp:LinkButton ID="LBtnEdit" runat="server" CausesValidation="False"
                     CommandName="Edit" Text="编辑"></asp:LinkButton>
                 &nbsp;<asp:LinkButton ID="LBtnDelete" runat="server" CausesValidation="False"
                     CommandName="Delete" Text="删除"  OnClientClick="return confirm('是否删除当前行?');"></asp:LinkButton>
             </ItemTemplate>
 </asp:TemplateField>

 

第二种方法就是在后台GridView的RowDataBound方法中写入

 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

    //为按钮写入onlick方法都有几种写法,都是大同小异。
            方法一:LinkButton deleteBtn = (LinkButton)e.Row.FindControl("LBtnDelete");

       if (deleteBtn != null)
              {
                deleteBtn.Attributes.Add("onclick", "return confirm('请确定要删除所选的数据项吗?');");
              }

    方法二:((LinkButton)e.Row.Cells[4].Controls[3]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[0].Text + "\"吗?')");

    方法三:((LinkButton)e.Row.Cells[4].FindControl("LBtnDelete")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[0].Text + "\"吗?')");

        }

转载于:https://www.cnblogs.com/linb/archive/2013/01/13/2858634.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值