首先看下面的代码片段,我们希望用户在点击页面上的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
>
< 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 >
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:
}
}
{
if (Page.IsPostBack)
{
// TODO:
}
相关资料:
http://www.4ucode.com/Study/Topic/1087401
http://wiki.operamasks.org/pages/viewpage.action?pageId=1835020
本文转自Jaxu博客园博客,原文链接:http://www.cnblogs.com/jaxu/archive/2011/10/25/2223947.html,如需转载请自行联系原作者