简体   繁体   中英

Using onClientClick and onClick together with form submission

So I'm having some trouble. Even after trying many different solutions my problem is still persistent.

The button click will fire the java script code, but not the server side code.

<asp:Button ID="btnSubmit" runat="server" CssClass="ins_sub_btn_form_map" OnClientClick="javascript: stripeSubmit(); return true;" onClick="btnSubmit_Click" 
                                        UseSubmitBehavior="false" ValidationGroup="Submit" Text="Submit" />

here's my javascript:

function stripeResponseHandler(status, response) {
            if (response.error) {
                // show the errors on the form
                $(".payment-errors").html(response.error.message);
            } else {
                var form$ = $("#form1");
                // token contains id, last4, and card type
                var token = response['id'];
                // insert the token into the form so it gets submitted to the server
                form$.append("<input type='visible' id='stripeToken' name='stripeToken' value='" + token + "' />");
                // and submit
                form$.get(0).submit();                    
            }
        }

        function stripeSubmit() {
            Stripe.setPublishableKey($("input#txtStripePK").val());
            var expDate = $("input#txtExpirationDate").val();
            if (Stripe.card.validateCardNumber($("input#txtCreditCardNumber").val()) == false)
            {
                alert("Credit Card Number Error");
                return;
            }
            if (Stripe.card.validateCVC($("input#txtCVVNumber").val()) == false) {
                alert("CVN Number Error");
                return;
            }
            if (Stripe.card.validateExpiry((expDate.slice(0, 2)) , ("20" + expDate.slice(2, 4))) == false) {
                alert("Expiration Date Error");
                return;
            }
                // createToken returns immediately - the supplied callback submits the form if there are no errors
                Stripe.card.createToken({
                    number: $("input#txtCreditCardNumber").val(),
                    cvc: $("input#txtCVVNumber").val(),
                    exp_month: (expDate.slice(0, 2)),
                    exp_year: ("20" + expDate.slice(2, 4))
                }, stripeResponseHandler);
                //return true; // submit from callback
            }

I've tried it without the return true in javascript, with it in javascript. Also tried it without the return true in the onClientClick, also without the "javascript:". Literally everything I can think of.

Any help is appreciated. Thanks in advance.

If I'm not mistaken, by default, asp control's UseSubmitBehavior property is set to true. This means any event performed on that control will cause a postback. You have UseSubmitBehavior set to false so the code behind will never get executed since no postback occurs. When using OnClientClick() and OnClick() together, the client side code will execute first and then the server side code. Hope this helps

Use closing () on the method inside your Onclick. ex onClick="SomeMethod()"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM