Posting from ASP.NET WebForms page to another URL
Join the DZone community and get the full member experience.
Join For FreeFew days ago I had a case when I needed to make FORM POST from my ASP.NET WebForms page to an external site URL. More specifically, I was working on implementing Simple Payment System (like Amazon, PayPal, MoneyBookers). The operator asks to make FORM POST request to a given URL in their website, sending parameters together with the post which are computed on my application level (access keys, secret keys, signature, return-URL… etc). So, since we are not allowed nesting another form inside the <form runat=”server”> … </form>, which is required because other controls in my ASPX code work on server-side, I thought to inject the HTML and create FORM with method=”POST”.
After making some proof of concept and testing some scenarios, I’ve concluded that I can do this very fast in two ways:
- Using jQuery to create form on fly with the needed parameters and make submit()
- Using HttpContext.Current.Response.Write to write the form on server-side (code-behind) and embed JavaScript code that will do the post
Both ways seemed fine.
1. Using jQuery to create FORM html code and Submit it.
Let’s say we have ‘PAY NOW’ button in our ASPX code:
<asp:Button ID="btnPayNow" runat="server" Text="Pay Now" />
Now, if we want to make this button submit a FORM using POST method to another website, the jQuery way should be as follows:
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.5.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#btnPayNow").click(function (event) {
event.preventDefault();
//construct htmlForm string
var htmlForm = "<form id='myform' method='POST' action='http://www.microsoft.com'>" +
"<input type='hidden' id='name' value='hajan' />" +
"</form>";
//Submit the form
$(htmlForm).appendTo("body").submit();
});
});
</script>
Yes, as you see, the code fires on btnPayNow click. It removes the default button behavior, then creates htmlForm string. After that using jQuery we append the form to the body and submit it. Inside the form, you can see I have set the htttp://www.microsoft.com URL, so after clicking the button you should be automatically redirected to the Microsoft website (just for test, of course for Payment I’m using Operator's URL).
2. Using
HttpContext.Current.Response.Write to write the form on server-side
(code-behind) and embed JavaScript code that will do the post
The C# code behind should be something like this:
public void btnPayNow_Click(object sender, EventArgs e)
{
string Url = "http://www.microsoft.com";
string formId = "myForm1";
StringBuilder htmlForm = new StringBuilder();
htmlForm.AppendLine("<html>");
htmlForm.AppendLine(String.Format("<body onload='document.forms[\"{0}\"].submit()'>",formId));
htmlForm.AppendLine(String.Format("<form id='{0}' method='POST' action='{1}'>", formId, Url));
htmlForm.AppendLine("<input type='hidden' id='name' value='hajan' />");
htmlForm.AppendLine("</form>");
htmlForm.AppendLine("</body>");
htmlForm.AppendLine("</html>");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(htmlForm.ToString());
HttpContext.Current.Response.End();
}
So, with this code we create htmlForm string using StringBuilder class and then just write the html to the page using HttpContext.Current.Response.Write. The interesting part here is that we submit the form using JavaScript code:
document.forms["myForm1"].submit()
This code runs on body load event, which means once the body is loaded the form is automatically submitted.
Note: In order to test both solutions, create two applications on your web server and post the form from first to the second website, then get the values in the second website using Request.Form[“input-field-id”]
Published at DZone with permission of Hajan Selmani, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Tech Hiring: Trends, Predictions, and Strategies for Success
-
Getting Started With Istio in AWS EKS for Multicluster Setup
-
Top 10 Engineering KPIs Technical Leaders Should Know
-
Seven Steps To Deploy Kedro Pipelines on Amazon EMR
Comments