The ASP.NET page is Shared with IFrame for solutions to problems encountered in submitting form data

  • 2020-05-16 06:38:21
  • OfStack

Looking at the code snippet below, we want the user to first submit the data to the specified third party page when he clicks on Button on the page, and then execute the Page_Load event in the background.
 
<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> 

The onsubmit event in Form is triggered when the page is submitted. At this point, the WebGatewayDoubleSubmission script method is first executed. In this method, the action, onsubmit, target and method of Form are cached into the specified variables. The submitPostIframeSubmission method is then called using IFrame, which is hidden on the page, and the original Form is submitted. There is a problem here, 1 line has been commented out in the above code, the reason is that IFrame's onload method can not trigger the event directly, so the submitPostIframeSubmission method can not execute, the second submission of the page was not successful! The eventPush method can effectively solve this problem.
Also, in the Page_Load event on the server side, IsPostBack needs to be used to determine whether the page has been submitted:
 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (Page.IsPostBack) 
{ 
//TODO: 
} 
} 

Relevant information:
http://www.4ucode.com/Study/Topic/1087401
http://wiki.operamasks.org/pages/viewpage.action?pageId=1835020


Related articles: