【问题标题】:How to add a "confirm delete" option in ASP.Net Gridview?如何在 ASP.Net Gridview 中添加“确认删除”选项?
【发布时间】:2011-06-15 04:52:58
【问题描述】:

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

【问题讨论】:

  • 您想在gridview按钮内部还是gridview外部添加“确认删除”选项?

标签: asp.net gridview


【解决方案1】:

这是我的首选方法。非常直接:

http://www.codeproject.com/KB/webforms/GridViewConfirmDelete.aspx

【讨论】:

    【解决方案2】:

    应该这样做。

    我在这里找到它: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>  
    

    【讨论】:

    • 如果您不想使用图像,那么您可以轻松地使用 LinkBut​​ton 而不是 ImageButton,如下面@Saksham-Gupta 的解决方案所示
    【解决方案3】:

    您可以使用 OnClientClick 按钮。

    OnClientClick="return confirm('confirm delete')"
    

    【讨论】:

    • 按钮没有 OnClientClick 事件。您可以为 LinkBut​​ton 添加 OnClientClick
    【解决方案4】:

    我做的有点不同。在我的网格视图中,我设置了AutoGenerateDeleteButton="true"。为了找到删除按钮,我使用 jQuery 并向找到的 Anchors 添加一个点击事件。

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

    这对我需要做的事情来说既快速又简单。请注意,页面中显示 Delete 的每个 Anchor 都将由 jQuery 选择并添加事件。

    【讨论】:

    • 效果很好。如果需要,您可以通过添加更具体的选择器以在网格中查找锚点来进一步改进。
    【解决方案5】:

    我在获取 commandField Delete 按钮以兑现“return false”响应时遇到问题,当用户单击“您确定”弹出窗口上的取消时,使用 javascript confirm() 函数会得到该响应。我不想将其更改为模板字段。

    在我看来,问题在于这些 commandField 按钮已经有一些与它们相关联的 Javascript 来执行回发。简单地附加 confirm() 函数是没有任何效果的。

    我是这样解决的:

    使用JQuery,我首先找到页面上的每个删除按钮(有几个),然后根据访问者是否同意或取消确认弹出来操作按钮的关联Javascript。

    <script language="javascript" type="text/javascript">
    $(document).ready(function() {
    
                   $('input[type="button"]').each(function() {
    
                       if ($(this).val() == "Delete") {
                           var curEvent = $(this).attr('onclick');
                           var newContent = "if(affirmDelete() == true){" + curEvent + "};" 
                           $(this).attr('onclick',newContent);                       
                       }
                   });
    }
    
    function affirmDelete() {    
                   return confirm('Are you sure?');
    }
    </script>
    

    【讨论】:

      【解决方案6】:

      试试这个:

      我用于更新删除按钮。它不会触及 Edit 按钮。您可以使用自动生成的按钮。

        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?');";
              }
      
          }
      

      【讨论】:

        【解决方案7】:

        如果您的 GridviewAutoGenerateDeleteButton="true" 一起使用,您可以将其转换为 LinkButton

        1. 单击GridView 任务,然后单击编辑列

        2. Selected fields中选择Delete,然后点击Convert this field into a TemplateField。然后点击确定

        3. 现在将生成您的LinkButton。您可以像这样将OnClientClick 事件添加到LinkButton
          OnClientClick="return confirm('Are you sure you want to delete?'); "

        【讨论】:

        • 感谢您指出“将此字段转换为模板字段”功能,我可以看到这在未来非常有用。
        【解决方案8】:

        我不想要任何图像,所以我修改了@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中确认删除的最简单方法
        【解决方案9】:

        我喜欢这种在从网格视图中删除记录之前添加确认提示的方式。这是嵌套在 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
        

        这似乎工作正常。

        【讨论】:

        • @statmaster 的回答比这个更好。首先,您只需要在 aspx 页面中放代码(这个需要在 aspx 页面和代码隐藏页面中都有代码)。其次,此解决方案需要 MsgBox,它可能无法在 Web 服务器上运行。您仍然可以使用 ScriptManager 在代码隐藏中显示警报,而不是使用 MsgBox,它可以在 Visual Studio 和 Web 服务器上工作。但是为什么不直接使用不需要任何代码隐藏的更简单的解决方案呢?
        【解决方案10】:

        这段代码对我来说很好用。

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

        【讨论】:

          【解决方案11】:

          我非常喜欢在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));
          
              }
          
          }
          

          【讨论】:

            【解决方案12】:

            我喜欢 JavaScript 解决方案,并且有一些更新可用于动态 ajax 加载:

                 $(document).on("click", "a", function () {
                    if (this.innerHTML.indexOf("Delete") == 0) {
                        return confirm("Are you sure you want to delete this record?");
                    }
                });
            

            希望对您有所帮助;)

            【讨论】:

              【解决方案13】:

              虽然这些答案中的许多都可以使用,但这显示了使用 OnClientClick 属性在 GridView 中使用 CommandField 时的一个简单示例。

              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?');";
                  }
              }
              

              【讨论】:

                【解决方案14】:

                这是我的方法,效果很好。

                asp

                 <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="5%" HeaderStyle-Width="5%" HeaderStyle-CssClass="color" HeaderText="Edit"
                                                EditText="<span style='font-size: 20px; color: #27ae60;'><span class='glyphicons glyph-edit'></span></span>"
                                                DeleteText="<span style='font-size: 20px; color: #c0392b;'><span class='glyphicons glyph-bin'></span></span>"
                                                CancelText="<span style='font-size: 20px; color: #c0392b;'><span class='glyphicons glyph-remove-2'></span></span>" 
                                                UpdateText="<span style='font-size: 20px; color: #2980b9;'><span class='glyphicons glyph-floppy-saved'></span></span>" />
                

                C#(将 5 替换为按钮的列号)

                        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                        {
                
                        }
                        else {
                            ((LinkButton)e.Row.Cells[5].Controls[2]).OnClientClick = "return confirm('Do you really want to delete?');";
                
                        }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2021-12-19
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-06-07
                  • 1970-01-01
                  • 2016-12-17
                  • 2021-04-15
                  • 1970-01-01
                  相关资源
                  最近更新 更多