【问题标题】:Delete Confirmation Message in CommandField?删除 CommandField 中的确认消息?
【发布时间】:2013-10-28 12:10:18
【问题描述】:

我正在尝试在 GridView 中的 delete 按钮上单击 时收到 confirmation 消息。如果我 conform 只有该行将在 GridView 中被删除。

*.ASPX

<Columns>

    <asp:CommandField ButtonType="Button" ShowDeleteButton="true" />

</Columns>

*.ASPX.CS

protected void grdPersTable_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Button buttonCommandField = e.Row.Cells[0].Controls[0] as Button;
        buttonCommandField.Attributes["onClick"] = 
               string.Format("return confirm('Are you want delete ')");
    }
}

protected void grdPersTable_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    Label lbl0 = (Label)grdPersTable.Rows[e.RowIndex].FindControl("lblId");
    txtId.Text = lbl0.Text;
    obj.DeleteV(Convert.ToInt32(txtId.Text));
    grdPersTable.DataSource = obj.GetTableValues();
    grdPersTable.DataBind();        
    lblMessage.Text = "Deleted successfully !";
}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    我得到了回答的朋友

    <asp:TemplateField>
          <ItemTemplate>
                <asp:Button ID="deletebtn" runat="server" CommandName="Delete" 
                 Text="Delete" OnClientClick="return confirm('Are you sure?');" />
          </ItemTemplate>
    </asp:TemplateField>
    

    我将 CommandField 更改为 TemplateField

    谢谢!

    【讨论】:

    • 您是否仍然可以在数据源上使用 DeleteCommand 功能,如果那是您调用删除 SQL 的地方 - 如果您使用的是 SQL?
    【解决方案2】:

    如下改变rowdatabound事件。

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((Button)e.Row.Cells[0].Controls[0]).OnClientClick = "return confirm('Are you sure you want to delete?');"; 
        }
    }
    

    【讨论】:

    • 我使用了上面的代码。我收到类似“无法将'System.Web.UI.LiteralControl'类型的对象转换为'System.Web.UI.WebControls.Button'的错误。”
    • 使用这个 ((LinkBut​​ton)e.Row.Cells[0].Controls[0]).OnClientClick = "return Confirm('Are you sure you want to delete?');";
    【解决方案3】:

    只需在 onclientclick 事件上调用 javascript 函数并请求确认。如果返回true,则可以调用服务端代码删除。

    下面是解释代码

    <asp:LinkButton ID="lbDelete" runat="server" OnClick="lbDelete_Click" OnClientClick="return fnConfirm();"> Delete</asp:LinkButton>
    

    下面是javascript函数:

    <script type="text/javascript">
    function fnConfirm() {
        if (confirm("The item will be deleted. Are you sure want to continue?") == true)
            return true;
        else
            return false;
    }
    </script>
    

    您可以在下面的链接中查看带有源代码的详细文章

    http://www.dotnetpickles.com/2013/03/how-to-show-confirm-message-while.html

    谢谢

    【讨论】:

      【解决方案4】:
      <asp:TemplateField HeaderText="DELETE" ShowHeader="False">
                              <ItemTemplate>
                              <span onclick="return confirm('Are you sure to Delete?')">
                                  <asp:Button ID="Button1" runat="server" CausesValidation="False"
                                      CommandName="Delete" Text="Delete" />
                              </ItemTemplate>
      </asp:TemplateField>
      

      【讨论】:

        【解决方案5】:

        Microsoft 的文档显示了如何使用 ASP DataSource 的 DeleteCommand 参数执行此操作的示例:

        https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.sqldatasource.deletecommand?view=netframework-4.8

        它使用 DataSource 类的“OnDeleting”参数和事件。

        【讨论】:

          猜你喜欢
          • 2013-08-17
          • 1970-01-01
          • 2020-02-26
          • 1970-01-01
          • 1970-01-01
          • 2013-06-07
          • 1970-01-01
          • 1970-01-01
          • 2021-12-05
          相关资源
          最近更新 更多