首先看下面的代碼片段,我們希望用戶(hù)在點(diǎn)擊頁(yè)面上的Button時(shí)首先將數(shù)據(jù)提交到指定的第三方頁(yè)面,然后再執(zhí)行后臺(tái)的Page_Load事件。
復(fù)制代碼 代碼如下:
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事件在頁(yè)面被提交時(shí)觸發(fā),此時(shí)首先執(zhí)行WebGatewayDoubleSubmission腳本方法,在該方法中,將當(dāng)前Form的action,onsubmit,target,method緩存到指定的變量中,然后將Form的action和target指向另一個(gè)頁(yè)面進(jìn)行提交,此時(shí)頁(yè)面上的數(shù)據(jù)被Post到第三方頁(yè)面。然后再使用頁(yè)面上隱藏的IFrame來(lái)調(diào)用submitPostIframeSubmission方法,并將原先的Form進(jìn)行提交。這里有一個(gè)問(wèn)題,在上面的代碼中有一行被注釋掉了,原因就是直接使用IFrame的onload方法并不能觸發(fā)該事件,從而導(dǎo)致submitPostIframeSubmission方法不能執(zhí)行,頁(yè)面的第二次提交不成功!使用eventPush方法可以有效地解決該問(wèn)題。
同時(shí),在服務(wù)端的Page_Load事件中,需要使用IsPostBack來(lái)判斷頁(yè)面是否被提交了:
復(fù)制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
//TODO:
}
}
相關(guān)資料:
http://www.4ucode.com/Study/Topic/1087401
http://wiki.operamasks.org/pages/viewpage.action?pageId=1835020
您可能感興趣的文章:- asp.net中MVC借助Iframe實(shí)現(xiàn)無(wú)刷新上傳文件實(shí)例
- asp.net在iframe中彈出信息并執(zhí)行跳轉(zhuǎn)問(wèn)題探討
- asp.net 文件上傳與刷新與asp.net頁(yè)面與iframe之間的數(shù)據(jù)傳輸
- ASP.NET中使用IFRAME建立類(lèi)Modal窗口
- asp.net省市三級(jí)聯(lián)動(dòng)的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
- asp.net 學(xué)習(xí)之路 項(xiàng)目整體框架簡(jiǎn)單的搭建
- asp.net GridView中超鏈接的使用(帶參數(shù))
- ASP.NET 鏈接 Access 數(shù)據(jù)庫(kù)路徑問(wèn)題最終解決方案
- asp.net實(shí)現(xiàn)生成靜態(tài)頁(yè)并添加鏈接的方法
- ASP.NET中iframe框架點(diǎn)擊左邊頁(yè)面鏈接 右邊顯示鏈接頁(yè)面內(nèi)容