I've found several posts that are similar but not quite what I'm trying to do. I have a Save button in my aspx. I have some logic in the event handler to check for certain conditions and if they're met, then I need a popup asking for confirmation to continue. As this is happening after some processing and not immediately following a button click, I'm calling it on the backend.
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"/>
This all works mostly fine. What I need to do next is on the backend I need to additional processing based on the HiddenField:
if (HiddenField1.Value == "True")
{
FinishProcessing();
}
else
{
// Do nothing
}
The issue is I have to click the button twice to get the results, like I need to do a postback to get the HiddenValue. I'd prefer not to do a postback such as with Server.Transfer as I need to retain several elements on the form, though I thought an UpdatePanel would solve that problem.
Alternate code I have tried:
JavaScript (Shows the correct values in the alert, but the same behavior as above is present)
<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>
Both sets of code yield the same issue. What am I doing wrong?

