Paypal button doesn't work with Masterpage

I have a paypal buy-now-button on the web, written in 2.0.  The buy-now-button works great if I am not using masterpage, once I put the button in the masterpage it doesn't work.  It just postback but does nothing.  Below is my code, please help.

In aspx:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<% =PayPalForm %>
<asp:Button ID="button1" runat="server" Text="Buy Now" />

In aspx.vb:

Protected Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
            Dim strPayPalForm As New StringBuilder
            strPayPalForm.Append("<form  action="""" method=""post"">")
            strPayPalForm.Append("<input type=""hidden"" name=""cmd"" value=""_xclick"">")
            strPayPalForm.Append("<input type=""hidden"" name=""business"" value="""">")
            strPayPalForm.Append("<input type=""hidden"" name=""item_name"" value=""Add Funds"">")
             strPayPalForm.Append("<input type=""hidden"" name=""amount"" value=""80"">")
            strPayPalForm.Append("<input type=""hidden"" name=""no_shipping"" value=""1"">")
            strPayPalForm.Append("<input type=""hidden"" name=""return"" value="""">")
            'strPayPalForm.Append("<input type=""hidden"" name=""rm"" value=""2"">")
            strPayPalForm.Append("<input type=""hidden"" name=""cancel_return"" value="""">")
            strPayPalForm.Append("<input type=""hidden"" name=""no_note"" value=""1"">")
            strPayPalForm.Append("<input type=""hidden"" name=""currency_code"" value=""USD"">")
            strPayPalForm.Append("<input type=""hidden"" name=""lc"" value=""CA"">")
            strPayPalForm.Append("<input type=""hidden"" name=""bn"" value=""PP-BuyNowBF"">")
            'strPayPalForm.Append("<input type=""hidden"" name=""shopping_url"" value="""">")
            strPayPalForm.Append("<input type=""hidden"" name=""notify_url"" value="""">")
            PayPalForm = strPayPalForm.ToString

            Dim jscript As String
            jscript = "<script>document.forms[0].submit();</script>"
            Page.ClientScript.RegisterClientScriptBlock(GetType(Button), "KeyClientBlock", jscript)
Who is Participating?
timandkidsConnect With a Mentor Commented:
I could be wrong, but I believe you are trying to place a <form> (the PayPal form) inside a <form> (the ASP.NET form), which isn't going to work correctly. You can have multiple forms on a page, but not nested forms.

You could handle this in one of three ways that I can think of...

Place all the variables into a URL string, as querystring variables, instead of as form variables. I believe PayPal lets you do this. In other words, when the button is clicked in your VB code, you would response.redirect to the paypal page with the variables appended to the querystring.

Or, in Javascript when the button is clicked, you could alter the ACTION of the ASP.NET <form> to go to the PayPal location instead of posting back to itself, which is the default behavior of every ASP.NET form, and then submit() it. You may also need to alter the __VIEWSTATE variable of the form, kind of like:

function ButtonClick()
 document.WhateverTheNameOfTheASPNETFormIs.action = "":
 document.WhateverTheNameOfTheASPNETFormIs.__VIEWSTATE = "":

Or, you could take over the response code and print a form to the screen and have it automatically submit onload, kind of like:

Response.Write("<form action="""" method=""post"" name=""PayPalForm"">")
Response.Write("<input type=""hidden"" name=""cmd"" value=""_xclick"">")
instead of writing all that code to write hidden fields in the click event, you might as well add all of those hidden fields during design time ..

do you have other form under
<% =PayPalForm %> ??

anyway, give this a try
strPayPalForm.Append("<form id='frmPayPal' action="""" method=""post"">")

give an id to the form and then

jscript = "<script>document.forms['frmPayPal'].submit();</script>"

Forgive me, but this line in my post above:
document.WhateverTheNameOfTheASPNETFormIs.action = "";

...may need to be:
document.WhateverTheNameOfTheASPNETFormIs.action.value = "";

...and the same for the .__VIEWSTATE line. Just add ".value" in there at the end.

Also, if you want the third option I mention above, you would put all that code into your VB button_click subroutine.

Finally, if your values that you are passing to PayPal are always the same (as they appear to be in your VB above), you could go with the first option I mentioned in the post above as just a static anchor tag:

<a href="">Click Here to Give Us Money</a>

...then you wouldn't need any VB to go along with it.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

cmleung2Author Commented:
I am not good with javascript, what is the WhateverTheNameOfThe ASPNETForms, do I need to assign a name first for the window?  I want to use the current window, would that be form[0]?  Thanks
cmleung2Author Commented:
using forms[0] works. Thanks.
I'm glad that worked. You could have also pulled up the form in IE and right-clicked and "view source" to see the NAME property of the <form>. ASP.NET will dynamically generate that name, I believe, but I am pretty sure it will always be the same. But if forms[0] works, then great.

Keep in mind the way you chose to do it, in altering the action of the ASP.NET form, EVERYTHING on the page will be posted to PayPal, not just "PayPal form fields". In other words, if you have a number of different form fields on the page and a number of different submit buttons for different purposes, EVERY SINGLE ONE OF THOSE FORM FIELDS AND THEIR VALUES will be posted to PayPal.

If the only form info you have on the page is PayPal stuff, then this is nothing to worry about. If, on the other hand, you have other form fields on the page, depending on what they are, it could conceivably be a big security compromise (such as if you are gathering people's SSNs for some reason -- you don't want that info to be posted to PayPal). If there is anything on that page that you DO NOT WANT PayPal TO KNOW ABOUT, then you should probably go with a different method, like the URL string or the Response.Write() idea, where you can better control exactly what gets posted to PayPal.

Just something to keep in mind.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.