Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paypal Form Ruins My ASP.NET webforms layout -> How to Solve?

I am a student who is doing up a simple website in asp.net. My problem is, I wish to integrate Paypal on one of the pages, but asp.net has the ridiculous <form runat="server"> that is getting in my way. I am building a simple site layout using blueprint css, a very basic three-column layout. However, I need my main content section to be able to use the paypal form (buy now button), and the other areas of the site to use user controls, which I presume requires them to be wrapped in that irritating form tag. In fact, I would like to have a sitemap path control at the top of the main section of the site: something very basic. How might I achieve that? My problem is: I can't put the Paypal button in the form, and I don't know how to shift a 4th div into place. I am not even sure how divs and forms stack on each other.

Could I have some help please?

The page with the problem is: http://clubofpep.org/sandbox/sandbox_Alumni.aspx.

like image 748
Raphael Avatar asked Sep 14 '11 16:09

Raphael


2 Answers

Contrary to popular belief, you can have more than one form on ASP.Net webforms pages. What you cannot do is have more than one form with runat="server", nest a second form inside ASP.Net's main form, or use asp.net server controls outside the main form.

Therefore, to integrate a separate paypal form with the rest of an asp.net webforms page, you have to make sure that you can put it either before or after all of the asp.net web controls on the page, and then edit the aspx markup to make sure your paypal form is completely outside of asp.net's form.

The other thing is that a quick web search shows a multitude of paypal controls written for asp.net that will work with the required asp.net form to submit the payment. You could always try one of those.

like image 149
Joel Coehoorn Avatar answered Nov 05 '22 08:11

Joel Coehoorn


namespace CustomForm
{
    public class GhostForm : System.Web.UI.HtmlControls.HtmlForm
    {
        protected bool _render;

        public bool RenderFormTag
        {
            get { return _render; }
            set { _render = value; }
        }

        public GhostForm()
        {
            //By default, show the form tag
            _render = true;
        }

        protected override void RenderBeginTag(HtmlTextWriter writer)
        {
            //Only render the tag when _render is set to true
            if (_render)
                base.RenderBeginTag(writer);
        }

        protected override void RenderEndTag(HtmlTextWriter writer)
        {
            //Only render the tag when _render is set to true
            if (_render)
                base.RenderEndTag(writer);
        }
    }
}

USAGE:

ASPX:

<%@ Register TagPrefix="CF" Namespace="CustomForm" Assembly="CustomForm" %>
<body>
    <CF:GhostForm id="mainForm" runat="server">
    ...
</body>

<img src="https://www.sandbox.paypal.com/en_US/i/btn/btn_xpressCheckout.gif"> <asp:Button ID="checkoutBtn" runat="server" OnClick="CheckButton_Click"
    Text="Checkout" Width="100" CausesValidation="false" /> 

Code-Behind:

protected void Page_Load(object sender, EventArgs e)
{
    ...
    // Workaround for PayPal form problem
    GhostForm mainForm = new GhostForm();
    mainForm.RenderFormTag = false;
    // Go ahead and submit to PayPal :)
}
like image 27
IrishChieftain Avatar answered Nov 05 '22 07:11

IrishChieftain