【发布时间】:2022-07-07 01:48:32
【问题描述】:
我发现了几篇相似但不完全是我想要做的帖子。我的 aspx 中有一个保存按钮。我在事件处理程序中有一些逻辑来检查某些条件,如果它们满足,那么我需要一个弹出窗口要求确认以继续。由于这是在一些处理之后发生的,而不是在单击按钮后立即发生的,所以我在后端调用它。
cs
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "MyFunction()", true);
JavaScript
<script type="text/javascript">
function MyFunction() {
if (confirm("Do you want to continue?") == true) {
document.getElementById('<%=HiddenField1.ClientID %>').value = "True";
} else {
document.getElementById('<%=HiddenField1.ClientID %>').value = "False";
}
}
</script>
aspx
<asp:HiddenField ID="HiddenField1" runat="server"/>
这一切都很好。我接下来需要做的是在后端我需要基于 HiddenField 进行额外处理:
if (HiddenField1.Value == "True")
{
FinishProcessing();
}
else
{
// Do nothing
}
问题是我必须单击按钮两次才能获得结果,就像我需要回发才能获得 HiddenValue。我不希望使用 Server.Transfer 进行回发,因为我需要在表单上保留几个元素,尽管我认为 UpdatePanel 可以解决这个问题。
我尝试过的替代代码:
JavaScript(在警报中显示正确的值,但存在与上述相同的行为)
<script type="text/javascript">
function MyFunction() {
if (confirm("Do you want to continue?") == true) {
document.getElementById('<%=HiddenField1.ClientID %>').value = "True";
alert(document.getElementById('<%=HiddenField1.ClientID%>').value);
document.getElementById('form1').submit();
} else {
document.getElementById('<%=HiddenField1.ClientID %>').value = "False";
alert(document.getElementById('<%=HiddenField1.ClientID%>').value);
document.getElementById('form1').submit();
}
}
</script>
aspx
<asp:UpdatePanel ID="UpdatePanelHidden" runat="server">
<ContentTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"/>
</ContentTemplate>
</asp:UpdatePanel>
两组代码都产生相同的问题。我做错了什么?
【问题讨论】:
标签: javascript c# asp.net