首先看下面的代码片段,我们希望用户在点击页面上的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:
}
}
复制代码 代码如下:
<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
标签:
IFrame,提交表单
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“ASP.NET页面借助IFrame提交表单数据所遇到问题的解决方法分享”评论...