HOWTO: Using Paypal buttons with ASP.net the easy way

A couple of years ago I wanted to add a donation button to an ASP.net website. Specifically I wanted a Paypal based donation button. It should have been really easy. Paypal will let you make buttons on their site and they’ll generate the code for you to put onto your own web page. But unfortunately… it’s just not really that easy.  I recently needed to do this again (just today actually) and couldn’t bring myself to use one of the old hacks I had done, and I finally found a super easy solution. I don’t understand why it was so hard to find this information, but I decided to write it up for all to see and use.

The problem is that the code that PayPal generate is html form code. The “action” of the form is a page on their website. The reason that this is a problem is because most people use masterpages within their ASP.net webpages and those masterpages typically have form tags already in them. Since webpages are only supposed to have one “form” per page then adding the PayPal generated form in the content area of a page that is wrapped within a form on the masterpage is just not going to work. What happens is that when the user clicks on the PayPal submit button it just posts back to local website instead of posting to PayPal. Very annoying.

I read about 20 web pages / tutorials today about how one can use PayPal generated buttons within an ASP.net, and I was about to finally give up and do it the old way when it occurred to me that maybe I could control the current page’s masterpage’s form’s action programatically. (By the way that was the most possessives in a row ever!)

So here’s the deal, and it is so easy. Let’s say that the PayPal code generated for you looks like:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
 <input type="hidden" name="cmd" value="_s-xclick">
 <input type="hidden" name="hosted_button_id" value="blablablabla">
 <table>
 <tr><td><input type="hidden" name="on0" value="Subscription Level">Subscription Level</td></tr><tr><td><select name="os0">
 <option value="Pro">Pro $5.00 USD</option>
 <option value="Allstar">Allstar $15.00 USD</option>
 <option value="Superstar">Superstar $25.00 USD</option>
 </select> </td></tr>
 </table>
 <input type="hidden" name="currency_code" value="USD">
 <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
 <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
 </form>

Go ahead and paste the generated code into your page wherever it is you want it to go. You don’t have to jump through any hoops here. Now go to your codebehind and add a couple of lines to the “PageLoad” method for the page in question. Here’s a super simple PageLoad with the two new lines:

protected void Page_Load(object sender, EventArgs e)
 {
 if (!IsPostBack) 
 {  } 
 else  {  } 
 Form.Action = "https://www.paypal.com/cgi-bin/webscr";  
Form.Method = "post"; 
}

That’s it! What we did was we just programmatically set our Masterpage’s form’s action and method to whatever the PayPal form’s action / method was. So, now, when you click on the PayPal button your MasterPage form will still be the one that handles it, but it will handle it the exact same way that the PayPal form would have. Enjoy!

Please let me know how it works for you!

9 Comments

  1. Great post.

    Working on a clients page with pay pal buttons and it works great…very easy solution.

  2. I’m glad this worked for you. I remember it was really annoying to me when I was dealing with it. That’s usually all I ever post about – the things that made me work harder than I should have had to. Thanks for commenting.

  3. Thank you so much for this solution using the Paypal button within an ASP.NET master page….. saved me bundles of time.

  4. I posted earlier, but I felt I have to restate: THANK YOU, THANK YOU, THANK YOU!!! This stopped me from pulling all my hair out!!! “Paypal button within an ASP.NET master page”

  5. Found many confusing articles and finally “the easy way” using asp.net …. The tough way was finding this article …THANKS!! — Now I will test it using different buttons….

  6. I remember how much this drove me crazy early on so I’m glad to hear it’s still helping people out! Good luck!

  7. Thank you so much for this. I’ve been looking for a solution for this issue for 2 weeks when I came across your site. This is way simpler than all the other solutions out there. Your solution was a huge help!

  8. Hi there. I used your solution with the previous version of Visual studio,

    ” if (!IsPostBack)
    { }
    else { }
    Form.Action = “https://www.paypal.com/cgi-bin/webscr”;
    Form.Method = “post”; ”

    which saved me heaps of time. Since I have upgraded to VS 2013 it not longer works and gives an error. Is there any way to overcome this?
    Many thanks
    Luke

  9. Luke, I don’t have an instance of VS2013 available to test this. I don’t have any reason to think it would stop working.

    Where in the process are you getting an error? Feel free to comment back with the error you are seeing and maybe pastebin the error. I’ll try to take a look.

    Also, if you figure out what it is, by all means PLEASE follow up on this post to say how you resolved it!

    Good luck!

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>