DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Posting from ASP.NET WebForms page to another URL

Posting from ASP.NET WebForms page to another URL

Hajan Selmani user avatar by
Hajan Selmani
·
Mar. 16, 11 · News
Like (1)
Save
Tweet
Share
46.01K Views

Join the DZone community and get the full member experience.

Join For Free

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”]

ASP.NET Form (document)

Published at DZone with permission of Hajan Selmani, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps Roadmap for 2022
  • ChatGPT: The Unexpected API Test Automation Help
  • Visual Network Mapping Your K8s Clusters To Assess Performance
  • Why Does DevOps Recommend Shift-Left Testing Principles?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: