ASP.Net WebForms + Paypal Form

2019-08-02 20:37发布

问题:

I am currently trying to integrate PayPal into our existing Web Application.

The Web Application is using ASP.NET WebForms.

Since in WebForms we have a form element around (almost) everything, I wonder how to integrate a PayPal Button which uses a form itself.

Is there any way to do it?

回答1:

Yes. I do it in a three step process:

  1. Remove the form tag from the PayPal code since you can't have nested forms.
  2. Add an ID, class, or something to the PayPal button to let you find it in a jQuery function (this example uses an id of paypalbutton).
  3. Add a jQuery script to catch the click and override the form post:

This may need to be modified to work with your specific code (but it may work as is):

$(document).ready(function() {
    $("#paypalbutton").on("click",function(n) {
        n.preventDefault();
        $("#aspnetForm").attr("action","https://www.paypal.com/cgi-bin/webscr");
        $("#aspnetForm").submit(); //or whatever your WebForms form element is called
    });
});