ASP.NET頁面藉助IFrame提交表單資料所遇到的問題

來源:互聯網
上載者:User

首先看下面的程式碼片段,我們希望使用者在點擊頁面上的Button時首先將資料提交到指定的第三方頁面,然後再執行背景Page_Load事件。

<body>
    <iframe id="WebGatewaySubmissionProcessor_IFrame" name="WebGatewaySubmissionProcessor_IFrame" style="display: none;"></iframe>
    <form onsubmit="javascript:if (typeof WebGatewayDoubleSubmission != 'undefined') {WebGatewayDoubleSubmission(this);}" id="Form1" runat="server">
    <div id="page">
            <asp:Button ID="BtnClientSend" runat="server"  />
   </div>
   <script type="text/javascript" id="WebGatewayScript">
    WebGatewayDoubleSubmission = function(o) {
        var oldAction = o.action;
        var oldOnSubmit = o.onsubmit;
        var oldTarget = o.target;
        var oldMethod = o.method;
        var iframeSubmisionTarget = document.getElementById("WebGatewaySubmissionProcessor_IFrame");

        var submitPostIframeSubmission = function() {
            o.action = oldAction;
            o.target = oldTarget;
            o.method = oldMethod;
            o.onsubmit = oldOnSubmit;
            o.submit();
        };
        /*iframeSubmisionTarget.onload = submitPostIframeSubmission;*/
        eventPush(iframeSubmisionTarget, 'load', submitPostIframeSubmission);

        o.action = "http://webgateway.hostedmscrm.com/V2/formprocessor.aspx";
        o.target = "WebGatewaySubmissionProcessor_IFrame";
        o.onsubmit = null;
        o.method = "POST";
        o.submit();
    };
    
    WebGatewaySubmission = function(o) {
        o.action = "http://webgateway.hostedmscrm.com/V2/formprocessor.aspx";
        o.method = "POST";
    };

    function eventPush(obj, event, handler) {
        if (obj.addEventListener) {
            obj.addEventListener(event, handler, false);
        } else if (obj.attachEvent) {
            obj.attachEvent('on' + event, handler);
        }
    }
   </script>
 </form>

</body>

Form中的onsubmit事件在頁面被提交時觸發,此時首先執行WebGatewayDoubleSubmission指令碼方法,在該方法中,將當前Form的action,onsubmit,target,method緩衝到指定的變數中,然後將Form的action和target指向另一個頁面進行提交,此時頁面上的資料被Post到第三方頁面。然後再使用頁面上隱藏的IFrame來調用submitPostIframeSubmission方法,並將原先的Form進行提交。這裡有一個問題,在上面的代碼中有一行被注釋掉了,原因就是直接使用IFrame的onload方法並不能觸發該事件,從而導致submitPostIframeSubmission方法不能執行,頁面的第二次提交不成功!使用eventPush方法可以有效地解決該問題。

同時,在服務端的Page_Load事件中,需要使用IsPostBack來判斷頁面是否被提交了:

protected void Page_Load(object sender, EventArgs e)
{
     if (Page.IsPostBack)
     {
         //TODO:
     }

}

相關資料:

http://www.4ucode.com/Study/Topic/1087401 

http://wiki.operamasks.org/pages/viewpage.action?pageId=1835020 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.