在我的编码中[编码我已经使用并且在不使用gridview按钮时工作]总是将confirmValue返回为False。
我想知道为什么会这样。
弹出窗口完全忽略了询问"INPUT"
编码行的是或否Continue with File Deletion?"
部分。它只是自动直接进入我的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>
的GridView:
<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在单击按钮时删除行中的行/文件
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
答案 0 :(得分:1)
为什么不在不使用任何其他表单字段(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>
GridView的:
<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>
请注意return
OnClientClick="return OnConfirm();"
代码背后:
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
内调用代码而不是ClientScript
,则需要使用ScriptManager
ScriptManager.RegisterStartupScript(Me, [GetType](), "DeleteMessage",
"<script type='text/javascript'>alert('Yes was selected. File was deleted!');
document.location.href='" + Request.Url.AbsoluteUri + "'; </script>", False)