如何在ASP.Net Gridview中添加“确认删除”选项?

38
如何在ASP.Net Gridview中添加“确认删除”选项?

你想在GridView按钮内部还是外部添加“确认删除”选项? - Pankaj Agarwal
14个回答

73

这应该可以解决问题。

我在这里找到了它:http://forums.asp.net/p/1331581/2678206.aspx

<asp:TemplateField ShowHeader="False">
    <ItemTemplate>
        <asp:ImageButton ID="DeleteButton" runat="server" ImageUrl="~/site/img/icons/cross.png"
                    CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this event?');"
                    AlternateText="Delete" />               
    </ItemTemplate>
</asp:TemplateField>  

1
如果您不想使用图像,则可以像下面@Saksham-Gupta的解决方案中所示一样,轻松地使用LinkButton而不是ImageButton。 - Eric Barr

30

如果您的Gridview使用了AutoGenerateDeleteButton="true"属性,您可以将其转换为LinkButton

  1. 点击GridView任务,然后选择编辑列https://www.flickr.com/photos/32784002@N02/15395933069/

  2. 选定字段中选择Delete,然后单击将此字段转换为TemplateField。 然后单击OKhttps://www.flickr.com/photos/32784002@N02/15579904611/

  3. 现在会生成您的LinkButton。 您可以像这样向LinkButton添加OnClientClick事件:
    OnClientClick="return confirm('您确定要删除吗?'); "


1
感谢指出“将此字段转换为TemplateField”的功能,我可以看到这在未来会非常有用。 - NullRef

14

我的做法有点不同。在我的GridView中,我设置了 AutoGenerateDeleteButton="true"。使用jQuery查找删除按钮并为找到的锚点添加点击事件。

jQuery("a").filter(function () {
        return this.innerHTML.indexOf("Delete") == 0;
        }).click(function () { return confirm("Are you sure you want to delete this record?"); 
});

对我需要做的事情来说,这非常快捷简单。只需注意,页面中显示“删除”的每个锚点都将被jQuery选择并添加事件。


工作得很好。如果需要,您可以通过添加更具体的选择器来查找网格内的锚点来进一步改进事物。 - Kevin Shea

7

我喜欢在gridview中删除记录之前添加确认提示的方式。这是aspx页面中嵌套在GridView web控件中的CommandField定义。这里没有什么花哨的东西,只是一个简单直接的Commandfield。

<asp:CommandField ShowEditButton="true" UpdateText="Save" ShowDeleteButton="True">
  <ControlStyle CssClass="modMarketAdjust" />
</asp:CommandField>

然后,我只需要在GridView控件的RowDeleting事件中添加一些代码即可。这个事件在行实际被删除之前触发,这允许您获得用户的确认,并在他最终不想取消时取消事件。以下是我放置在RowDeleting事件处理程序中的代码:

Private Sub grdMarketAdjustment_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles grdMarketAdjustment.RowDeleting
  Dim confirmed As Integer = MsgBox("Are you sure that you want to delete this market adjustment?", MsgBoxStyle.YesNo + MsgBoxStyle.MsgBoxSetForeground, "Confirm Delete")
  If Not confirmed = MsgBoxResult.Yes Then
    e.Cancel = True 'Cancel the delete.
  End If
End Sub

看起来这样做很好。


1
@statmaster的答案比这个更可取。首先,您只需要将代码放在aspx页面中(此解决方案需要在aspx页面和代码后台页面中都需要代码)。其次,此解决方案需要使用MsgBox,而它可能无法在Web服务器上运行。您仍然可以使用ScriptManager在代码后台中显示警报,而不是使用MsgBox,并且它将在Visual Studio和Web服务器上均可工作。但为什么不使用更简单的解决方案,它不需要在代码后台中添加任何代码呢? - James Balentine
如果我需要从服务器端获得确认消息怎么办?例如,用于支持不同语言。@statmaster的解决方案在<% %>中不起作用。 - Siyon DP

5

我不需要图片,因此我修改了@statmaster提供的答案,使它成为一个简单的输入,与其他列一起使用。

<asp:TemplateField ShowHeader="False">
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this entry?');">Delete </asp:LinkButton>             
        </ItemTemplate>
</asp:TemplateField>

使用Forecolor属性可以更改文本的颜色。


这是在GridView中进行带确认的最简单的删除方式。 - JoshYates1980

4
你可以使用按钮的OnClientClick属性来实现。
OnClientClick="return confirm('confirm delete')"

按钮没有 OnClientClick 事件。您可以为 LinkButton 添加 OnClientClick。 - Ashin

3

我很喜欢在 GridViewRowDataBound 事件中添加代码,以告知用户所尝试删除的确切项目。这会稍微提高用户体验?

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        LinkButton lnkBtnDelete = e.Row.FindControl("lnkBtnDelete") as LinkButton;

        // Use whatever control you want to show in the confirmation message
        Label lblContactName = e.Row.FindControl("lblContactName") as Label;

        lnkBtnDelete.Attributes.Add("onclick", string.Format("return confirm('Are you sure you want to delete the contact {0}?');", lblContactName.Text));

    }

}

3
尝试以下操作:
我使用了“更新”和“删除”按钮。它不会触及“编辑”按钮。您可以使用自动生成的按钮。
  protected void gvOperators_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType != DataControlRowType.DataRow) return;
        var updateButton = (LinkButton)e.Row.Cells[0].Controls[0];
        if (updateButton.Text == "Update")
        {
            updateButton.OnClientClick = "return confirm('Do you really want to update?');";
        }
        var deleteButton = (LinkButton)e.Row.Cells[0].Controls[2];
        if (deleteButton.Text == "Delete")
        {
            deleteButton.OnClientClick = "return confirm('Do you really want to delete?');";
        }

    }

2

1
尽管这些答案中的许多都可以使用,但这个示例展示了在GridView中使用CommandField并使用OnClientClick属性时的简单示例。
ASPX:
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"... >
<Columns>
<!-- Data columns here -->
        <asp:CommandField ButtonType="Button" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150" />
</Columns>
</asp:GridView>

ASPX.CS:

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView1.EditIndex)
    {
        (e.Row.Cells[2].Controls[2] as Button).OnClientClick = "return confirm('Do you want to delete this row?');";
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接