Posting from ASP.NET WebForms page to another URL

Few days ago I had a case where I needed to make FORM POST from my ASP.NET WebForms page to another 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

Published Wednesday, March 16, 2011 2:16 AM by hajan

Comments

# re: Posting from ASP.NET WebForms page to another URL

Wednesday, March 16, 2011 2:46 AM by crusher

Pingback from  Posting from ASP.NET WebForms page to another URL - Hajan's Blog

# re: Posting from ASP.NET WebForms page to another URL

Thursday, March 17, 2011 3:42 PM by hajan

@Phil, interesting approach and very similar to the one I showed. The only thing is that you use plain JavaScript, so if someone don't use jQuery and want to do this using JavaScript, this is good to go with... otherwise the jQuery method seems cleaner ;). I also like the C# way of doing this.

# re: Posting from ASP.NET WebForms page to another URL

Wednesday, June 15, 2011 5:44 AM by fashion

very nice and informative post... thanx for sharing.

# re: Posting from ASP.NET WebForms page to another URL

Monday, October 31, 2011 2:17 PM by putrgek

This was a major help in getting my order information saved to my database and then depending on information in the order, automatically posting to the correct payment gateway.  Thanks for the help.

# re: Posting from ASP.NET WebForms page to another URL

Friday, December 16, 2011 9:18 AM by Bora

It works perfectly.

But ı had to change id with name ==> htmlForm.AppendLine(String.Format("<form NAME='{0}' method='POST' action='{1}'>", formId, Url));

# re: Posting from ASP.NET WebForms page to another URL

Sunday, December 18, 2011 7:13 AM by hajan

@Bora, that's fine ;)

# re: Posting from ASP.NET WebForms page to another URL

Saturday, April 28, 2012 5:50 AM by Minaxi

hi frnds ... i want to send a link of page to the server ... on clicking of that link i can return back to my site... is it possible? if yes please send me a code as soon as possible...

# re: Posting from ASP.NET WebForms page to another URL

Tuesday, August 7, 2012 2:51 PM by max

nice post , thanks

# re: Posting from ASP.NET WebForms page to another URL

Friday, August 9, 2013 3:56 PM by James

Option#2 may not be useful for PCI compliance because the form data is submitted back to the host server before posting to a payment gateway. But Option#1 helps.

Thanks for sharing.

Leave a Comment

(required) 
(required) 
(optional)
(required)