I recently started work on my first website to integrate with PayPal. The client needed it done relatively quickly. What started out for me as a mental picture of a products Web form with a "Buy Now" button, somehow turned into a full-blown e-commerce application complete with custom shopping cart. Talk about feature creep! And most of it was my own fault because I failed to anticipate the minimal requirements for the job. I'm still in the thick of it and have just implemented an admin back-end for the merchant to add products, complete with images, to the database. Then, I had to create an HttpHandler to stream the images...

They do say it's the simple things that get you stumped. For all of you ASP.NET developers out there who have yet to integrate a site with PayPal, just wait until you finally get to add that button to your form to pass the transaction details over to PayPal... In short, it won't work! The button HTML from the PayPal site is embedded in a form tag. You can only have one form on an ASP.NET page and ASP.NET provides its own. If you have a master page, then the form tag is in there and it is applied to every page in the site when they are merged with the master at runtime.

PayPal support does not offer a practical solution. They may try to get you to download their ASP.NET SDK which is 1.1 and uses Web Services. Most people have failed to get it to work with 2.0. Then they may tell you to put the form tag "outside" the main tag or on a separate HTML page, etc. I have seen endless hacks, most of which were too stupid to even consider; IFrames anyone?!!

I trudged through the forums and saw that ASP.NET developers have been asking how to get around this for the last three years or so. PayPal refuses to acknowledge the problem and seem more inclined to offer support for the PHP community. There is something radically wrong with this mindset from a business point of view. Can PayPal not afford to pay some contractors to go in and develop an ASP.NET 2.0 SDK that will work with both NVP and Web Services? Nothing like speed to kill. Then PayPal had the temerity to invite me to complete a survey on how good I found their support service...

So I turned to Google. I spent days concocting search strings that would bring that elusive nugget I needed to solve the problem. I thought I had found it when I came across the nested master page hack - keep the outer master page stripped of any form tag and then just use it for the page with the PayPal button. It would probably work, but if you don't get a code smell from that one, you may need to get your sinuses reamed out. The search continued. You know you're desperate when you start entering your grannie's middle name in the search query string :-O

Persistence finally paid off. I found an elegant solution on Jeremy Schneider's blog that consists of a custom HtmlForm class that can have the form tag rendering toggled on and off. The class is called GhostForm and has a property, RenderFormTag. When RenderFormTag is set to false, it doesn't render the opening or closing tags, but does render all of the contents. Reference the custom GhostForm class and in the code-behind of the form on which you are placing the button, place the following in the Page_Load to disable the master page form tag:

I'm wondering which PayPal product were you successful in using? I have an ASP application that I have all the buyer info, product cost, shipping and taxes already calculated, and I just want PayPal to handle the credit card or PP charge processing. I naively thought it would be a simple call to PP passing these items as arguments, but no such luck. I would like to use Website Payments Standard - it seems like it would work for me, but I'm not sure... Does anyone have any suggestions, and would this "elegant solution" you discuss really work? Thanks for any help.

I simply used the Ghost Form class to get around the ASPNET/PayPal form issue. I wrote some custom code with a cart and hooked up to PayPal in accordance with the Website Payments Standard. Check my other post here:
www.codersbarn.com/.../...tton-with-ASPNET-20.aspx

I had this problem, but figured out a way around it. In the Master Page's Page_Load() function, I pulled the current page without the extension, then checked the variable for the page which the PayPal button was on:

I tried your suggestion and get a bunch of build errors in vs2010 ( \Visual Studio 2010\WebSites\WebSite1\Site.master.cs(16,13): error CS0103: The name 'form1' does not exist in the current context). Question is how to reference the specific "paypal" form that I am interested in?