I have tried multiple solution suggestions for this issue, but none seem to be working as intended. My goal is to trigger a JavaScript validation function before running the C# code behind method when the validation returns true. The OnClick should follow the operation of the javascript OnClientClick.
Some of the solutions I attempted can be found in these threads:
Executing OnClick code behind method after returning true from OnClientClick javascript function
Executing OnClick code behind method after returning true from OnClientClick javascript function
This is my current setup:
<asp:Button ID="btnAdd" runat="server" AutoPostBack="true" OnClick="btnAdd_Click" OnClientClick="btnAdd_Click(this, event);" Text="Add" UseSubmitBehavior="false"/>
function btnAdd_Click(sender, args) {
if(confirm('Do you want to do a server trip?'))
{
return true;
}
else
{
return false;
}
}
I also attempted:
<asp:Button ID="btnAdd" runat="server" AutoPostBack="true" OnClick="btnAdd_Click" OnClientClick="if (!Confirm()) return false;" Text="Add" UseSubmitBehavior="false"/>
function Confirm() {
return confirm("Are you sure?");
}
Currently, I am experiencing an issue where the JavaScript always fires regardless of the outcome. Upon debugging, it shows that the JavaScript is indeed returning true to my solution, so the OnClick should be triggered. However, the break point in my click event on the code behind is never reached. Is there a site setting or some other factor preventing this from happening?
Any help would be greatly appreciated. Thank you in advance.