有确认删除的gridview(vs2005 asp.net)

Sometime when we delete the data with GridView,there needs a confirm dialog to get the user's operate.if confirm to delete,let gridview delete the right data.else not.
HTML页面中代码:

<asp:GridView DataKeyNames="CategoryID" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("CategoryID") %>' CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

上 面代码中设置了linkbutton,指定了commandname为"Delete",commandargument为要删除的记录的ID编号,一旦 commandname设置为delete这后,gridview中的GridView_RowCommand 和 GridView_Row_Deleting 事件都会被激发,处理其rowdatabound事件中:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
  l.Attributes.Add("onclick", "javascript:return " + "confirm('是否要删除该记录? " +
  DataBinder.Eval(e.Row.DataItem, "id") + "')");
 }
}
首先检查是否是datarow,是则得到每个linkbutton,再为其添加客户端代码。

当用户选择了确认删除后,在row_command事件中写代码:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
 if (e.CommandName == "Delete")
 {
  int id = Convert.ToInt32(e.CommandArgument);
  // 删除记录的专门过程
  DeleteRecordByID(id);
 }
}

if you wanna some special button have the same effect.you can try this method:
btnSpecial.Attributes.Add("onclick","javascript:return confirm('确认删除吗?');");
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值