Paypal button doesn't work with Masterpage

Posted on 2007-10-18
Last Modified: 2013-11-29
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)
Question by:cmleung2
    LVL 26

    Expert Comment

    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>"

    LVL 1

    Accepted Solution

    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"">")
    LVL 1

    Expert Comment

    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.
    LVL 1

    Author Comment

    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
    LVL 1

    Author Comment

    using forms[0] works. Thanks.
    LVL 1

    Expert Comment

    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.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now