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

后端 未结 2 1963
慢半拍i
慢半拍i 2021-01-01 03:25

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

相关标签:
2条回答
  • 2021-01-01 03:48

    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.

    0 讨论(0)
  • 2021-01-01 03:48
    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 :)
    }
    
    0 讨论(0)
提交回复
热议问题