cancel button not redirecting to separate page
-
27-10-2019 - |
Question
I have an ASP.NET form with a cancel button that is supposed to, after confirmation from the user, redirect them to another page. I'm using javascript for this. It works fine in a simple HTML page, but apparently something is interfering with it on the .aspx page. The popup message works perfectly, but clicking "Okay" does not take you to another page. I have tested the if statement and it is working correctly, the only thing it won't do is leave the current page.
My javascript:
<script type="text/javascript">
<!--
function confirmation() {
var answer = confirm("Are you sure you want to cancel? Any information you have entered will be discarded.")
if (answer) {
window.location = "index.htm";
}
}
-->
</script>
My button:
<asp:Button ID="btnCancel" runat="server" Text="Cancel" onClientClick="return confirmation();" CausesValidation="false" />
Solution
Set UseSubmitBehavior property of the button control to false:
<asp:Button UseSubmitBehavior="false" ID="btnCancel" runat="server" Text="Cancel" onClientClick="return confirmation();" CausesValidation="false" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow