user710502
user710502

Reputation: 11471

Cancelling all events using javascript

I have a asp link

<asp:LinkButton ID="next" CssClass="Button Large" runat="server" OnClick="Next_Click" OnClientClick="showBillingRequiredState(this)">Next</asp:LinkButton>

When its clicked I want to cancel the page from continue , I want it to just cancel processing if my javascript return false. Right now I am showing an error but then the error dissapears and it doesnt stick.. something is causing it refresh and I lose my error. Here is the javascript, as I mentioned it passes fine through all my conditions and it shows the div, but then after a second it dissapears.

 function showrequired(evt) {
        var code = document.getElementById('<%=codelist.ClientID%>').options[document.getElementById('<%=codelist.ClientID%>').selectedIndex].value;
        var currentValue= document.getElementById('<%=statevalue.ClientID%>').value;
        var eDiv = document.getElementById('reqDiv');

        if (code == "US" && currentValue.trim() == '') {
            eDiv.style.display = 'block';
            return false;
        }
        else {
            eDiv.style.display = 'none';
        }

     }

Upvotes: 2

Views: 65

Answers (2)

Akhil Sekharan
Akhil Sekharan

Reputation: 12683

Try:

 OnClientClick=" return showBillingRequiredState(this)"

Upvotes: 2

COLD TOLD
COLD TOLD

Reputation: 13579

jquery event default works really well in such situation

<script>
$("#<%= next.ClientID %>").click(function(event) {

 var countryCode = document.getElementById('<%=lstBillingCountryCode.ClientID%>').options[document.getElementById('<%=lstBillingCountryCode.ClientID%>').selectedIndex].value;
        var currentStateValue = document.getElementById('<%=txtBillingState.ClientID%>').value;
        var errorDiv = document.getElementById('reqBillingState');

        if (countryCode == "US" && currentStateValue.trim() == '') {
            errorDiv.style.display = 'block';
           event.preventDefault();
           //while stepping through this with firebug, it gets here fine but then it just gets ignored
        }
        else {
            errorDiv.style.display = 'none';
        }



});
</script>

Upvotes: 0

Related Questions