I have the following button on a WebForms page:
<asp:Button ID="btnNewProgramNext" runat="server" Text="Next >>" />
And I have the following javascript/jquery to handle it's event.
$('#<%= btnNewProgramNext.ClientID %>').on('click', function (e) {
var errorMessage = '';
var ok = false;
var newCompany = $('#<%= NewCompanyMode.ClientID %>').val();
if (newCompany == 1) {
if (!$('#<%= txtProgramCompanyName.ClientID %>').val())
errorMessage = 'You must enter a company name.';
else if (!$('#<%= txtContactFirstName.ClientID %>').val())
errorMessage = 'You must enter a contact first name.';
else if (!$('#<%= txtContactLastName.ClientID %>').val())
errorMessage = 'You must enter a contact last name.';
else
ok = true;
}
else {
// Do something else
ok = true;
}
$('#newProgramErrorMessage').text(errorMessage);
return ok;
});
The problem is, when this handler returns false (either when ok
is false, or when I change the last line to return false
), the postback still occurs.
Is there any trick to preventing a postback from javascript? I expected this to work.