3

我正在尝试在单击GridView中的删除按钮 时收到确认消息。如果我只符合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 !";
}
4

5 回答 5

7

我得到答案的朋友

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

我将CommandField更改为TemplateField

谢谢 !

于 2013-10-28T13:11:22.933 回答
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?');"; 
    }
}
于 2013-10-28T12:36:11.877 回答
1

只需在 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

谢谢

于 2014-02-13T05:04:31.903 回答
0
<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>
于 2016-04-11T10:57:06.457 回答
0

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

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

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

于 2021-02-01T11:55:27.770 回答