Paypal Donate Button Doesn't Open In ASP.Net Web site

The html code that Paypal provides for donate buttons doesn't work when inserted directly into an ASP.Net web form. Because the Paypal form is nested inside the main <form> tag, when we click the button the page posts back to itself and effectively just refreshes.

One way to get around this is to put the Paypal form outside the ASP.Net form (multiple forms are allowed on the same page in ASP.Net as long as only one form has the runat="server" attribute) but this is not an option when using ASP.Net Master Pages.

Because I was using Master Pages, I needed the donate button to appear inside the ASP.Net form. I found a workaround using the <iframe> element. All we do is put the Paypal form in it's own html page within our site eg. siteroot/paypal.htm, then define the iframe element where we want the button to appear, and specify it's source attribute to be src="siteroot/paypal.htm".

I had to set the target of the Paypal form to be target="_blank" so it would open in a new window (you could use target="_top" to have the link open in the parent browser window, not the iframe itself). I also had to set some style attributes on the iframe to hide the border and set it's width height etc.

It works a treat! Example below.

Jimmy

<iframe frameBorder="0" style="width:80px;height:25px;" src="paypal.htm"></iframe>

And paypal.htm (the only thing i modified was the target of the form tag):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Donate</title>
</head>
<body style="padding:0px; margin:0px;">
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_blank">
    <input type="hidden" name="cmd" value="_s-xclick">
    <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
    <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
    <input type="hidden" name="encrypted" value="...">
</form>
</body>
</html>

Related posts

Comments

Add comment


 

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

September 9. 2010 09:34

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010