Posting from ASP.NET WebForms page to another URL

Posted by hajan on ASP.net Weblogs See other posts from ASP.net Weblogs or by hajan
Published on Wed, 16 Mar 2011 00:16:00 GMT Indexed on 2011/03/16 8:10 UTC
Read the original article Hit count: 620

Filed under:
|
|
|
|

Few 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:

  1. Using jQuery to create form on fly with the needed parameters and make submit()
  2. 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”]

I hope this was useful post for you.

Regards,
Hajan

© ASP.net Weblogs or respective owner

Related posts about ASP.NET

Related posts about jQuery