When Written: Dec 2009
Whilst building an ASP .NET web site the other day we had a need to put a PayPal ‘pay now’ button, which is normally simple enough. The web site of the provider ( PayPal in this case ) provides a wizard that creates the HTML code for the button and the associated <FORM> tag that you simply copy and paste in to your web page. However with ASP .NET all the web pages have a <FORM> in them already to deal with user clicks and asynchronous postbacks to the server.
If you use Master Pages for applying common code to your web pages it is normal for the <FORM> tag to be in this Master Page which means that you can’t easily remove it and you can’t have one form embedded within another. So what do you do? A look round the forums showed that this is a common problem and like most problems there are more than one solution. Some of these verged from nasty ‘kludges’ of removing the Master Page and manually adding the code that was in the Master Page to the web page with the troublesome button on, but this would then mean that the web page would not update automatically if any changes were made to the Master Page, not at all ideal.
Another solution was very slick in that it created a FORM object in memory and the button exists in this, so when clicked the action of this form would be committed. The code to achieve this was fairly involved, and it was at this moment that I came across a different and much simpler solution. All you need to do is to remove the form code around the button and change the ‘PostbackUrl’ on the asp:imagebutton to the url defined in the removed form’s ‘action’ so when a user clicks this button a submit type event is fired and the data on the form is sent via a ‘POST’ to this url.
The slight drawback with this method is that all the data from any other form objects on the page would also be sent to this URL but that is not normally a problem. I do like simple solutions.
Article by: Mark Newton
Published in: Mark Newton