Gridview 按钮总是删除 returns False

Gridview Button delete always returns False

在我的编码中 [我使用过并且在不使用 gridview 按钮时有效的编码] 总是 returns 我的 confirmValue 为 False。

我想知道为什么会这样。 弹出窗口完全忽略了询问是否 Continue with File Deletion?" 代码行的 Yes 或 No "INPUT" 部分。它会自动直接进入我的 Vb.net 编码,确认值为 false。

有什么建议吗?

JavaScript

<script type = "text/javascript">

function onConfirm() {

    var confirm_value = document.createElement("INPUT");

    confirm_value.type = "hidden";

    confirm_value.name = "confirm_value";

    if (confirm("Continue with File Deletion?")) {

        confirm_value.value = "Yes";
        //alert("Yes was Selected. Article Selected Will Be Deleted!")

    } else {

        confirm_value.value = "No";
        //alert("No was Selected. Article Will Not Be Deleted.")
    }

    document.forms[0].appendChild(confirm_value);

}

</script>

网格视图:

<asp:TemplateField HeaderText = "Delete File"> 
   <ItemTemplate >
      <asp:button ID = "btnDelete" cssClass = "submit-btn small" Text = "Delete" CommandArgument = '<%# Eval("Value") %>' runat = "server" OnClientClick = "OnConfirm()" OnClick = "DeleteFile" />
   </ItemTemplate>
   <ItemStyle cssClass="grid_padding" />

</asp:TemplateField>

Vb.net点击按钮时删除行中的row/file

Protected Sub DeleteFile(ByVal sender As Object, ByVal e As EventArgs)

    Dim confirmValue As String = Request.Form("confirm_value")

    If confirmValue = "Yes" Then
        Response.Write("<script language=""javascript"">alert('Yes was Selected. File Will Be Deleted!');</script>")
        Dim filePath As String = CType(sender, Button).CommandArgument
        File.Delete(filePath)
        Response.Redirect(Request.Url.AbsoluteUri)
    Else
        Response.Write("<script language=""javascript"">alert('No was Selected. File Will Not Be Deleted.');</script>")
    End If

End Sub

为什么不使用任何额外的表单域(confirmValue):

JavaScript:

<script type = "text/javascript">
function OnConfirm() {
    if (confirm("Continue with File Deletion?")) {
        return true;
    } else {
        alert("No was Selected. Article Will Not Be Deleted.");
        return false;
    }
}
</script>

网格视图:

<asp:TemplateField HeaderText = "Delete File"> 
   <ItemTemplate>
      <asp:button ID = "btnDelete" cssClass = "submit-btn small" Text = "Delete" CommandArgument = '<%# Eval("Value") %>'
          runat = "server" OnClientClick = "return OnConfirm();" OnClick = "DeleteFile" />
   </ItemTemplate>
   <ItemStyle cssClass="grid_padding" />
</asp:TemplateField>

OnClientClick="return OnConfirm();"

中通知return

后面的代码:

Protected Sub DeleteFile(ByVal sender As Object, ByVal e As EventArgs)
    Dim filePath As String = CType(sender, Button).CommandArgument
    File.Delete(filePath)
    Page.ClientScript.RegisterStartupScript(Me.GetType(), "DeleteMessage",
       "<script type='text/javascript'>alert('Yes was selected. File was deleted!');
       document.location.href='" + Request.Url.AbsoluteUri + "'; </script>")
End Sub

此外,如果您从 UpdatePanel 中调用后面的代码,那么您需要使用 ScriptManager

而不是 ClientScript
ScriptManager.RegisterStartupScript(Me, [GetType](), "DeleteMessage",
    "<script type='text/javascript'>alert('Yes was selected. File was deleted!');
    document.location.href='" + Request.Url.AbsoluteUri + "'; </script>", False)