I need to use a JavaScript Confirm function in my ASPX pages to confirm various actions based on conditions in the C# code behind not directly connected to a button click event. E.g. if it is calculated that number of records > 200, ask 'Do you want to continue?' then based on Yes or No clicked perform relevant actions. I have my JavaScript defined as:
<script type = "text/javascript">
function Confirm(val) {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm(val)) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
And call it from code behind and get the response using RegisterStartupScript and Request.Form like so:
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "confirm_value", String.Format("Confirm('{0}');", msgstr), true);
string confirmvalue = Request.Form["confirm_value"];
The Confirm box comes up, but the confirmvalue string is always "behind". That is, if I click 'Yes' on the Confirm box, it returns 'No'; but if I stay on the same page and execute the process a second time and click 'No', I get 'Yes' returned; and so on. Question: How do I force postback of confirmvalue so I can access the response in code behind in a timely manner?