Introduction
Sometime you need to post a form to an differenturl from asp.net pages, for example you might need to send user to third party payment processing system using post method, asp.net does not provide any straight forward way to accomplish this task.
Problem which most users faces with server side form in aspx page are, you are not allowed to change action of form and you are allowed to use only one server side form per page.
Possible Solutions
- One possible solution to this problem is to Create your own form control and use it on page this will allow you to change action of form, but again what if you do not want some existing input elements in current page to go to post.
- There is good way to post form data using HttpWebResponse & HttpWebRequest class if you want to post data behind the scenes, but if you want to post data using user browser then you are stuck.
Our Solution
I will try to show you one possible way to accomplish this task, we will create
- component that will create form with required fields and post the form to specified url,
- web page that will use that component to post data and
- page which will receive that data and display posted data.
A) RemotePost Class.
- public class RemotePost
- {
- private System.Collections.Specialized.NameValueCollection Inputs = new System.Collections.Specialized.NameValueCollection();
- public string Url = "";
- public string Method = "post";
- public string FormName = "form1";
- public void Add(string name, string value)
- {
- Inputs.Add(name, value);
- }
- public void Post()
- {
- System.Web.HttpContext.Current.Response.Clear();
- System.Web.HttpContext.Current.Response.Write("");
- System.Web.HttpContext.Current.Response.Write(string.Format("", FormName));
- System.Web.HttpContext.Current.Response.Write(string.Format("", FormName, Method, Url))
- for (int i = 0; i < Inputs.Keys.Count; i++)
- {
- System.Web.HttpContext.Current.Response.Write(string.Format("", Inputs.Keys[i], Inputs[Inputs.Keys[i]]));
- }
- System.Web.HttpContext.Current.Response.Write("");
- System.Web.HttpContext.Current.Response.Write("");
- System.Web.HttpContext.Current.Response.End();
- }
- }
- "Url" which is action of our form.
- "Method" which is Method of our form, default is Post but you can also use Get
- "FormName" which is name of form.
Methods of our component.
- "Add" which will be used to add form input name and value. and
- "Post" which will render html on page to do actual posting, most important part of this method is onload event of rendered html's body which will post form to specified URL.
and private field Inputs which will hold name value pair collection of all inputs that goes into form.
you can compile this class to dll and use in your project but for simplicity I am including that class directly into page itself.
B) Sample Page.
Following is sample page code which posts form to specified url.
- RemotePost myremotepost = new RemotePost();
- myremotepost.Url = http://www.jigar.net/demo/HttpRequestDemoServer.aspx;
- myremotepost.Add("field1","Huckleberry");
- myremotepost.Add("field2","Finn");
- myremotepost.Post() ;
Following is sample page code which posts form to specified url.
This is the page where posting will occur for simplicity we will just write posed value so that we can know what was posted.
- <%@ Page Language="C#" %>
- <script runat="server">
- void Page_Load(object sender, EventArgs e){
- if (Request.Form["field1" != null ){
- Response.Write("field1 : " + Request.Form["field1" + "")}
- if(Request.Form["field2" != null ){
- Response.Write("field2 : " +Request.Form["field2" + "")}
- }
- </script>
Click "http://www.jigar.net/demo/RemotePost.aspx" target="new">here to run sample
There will be cases where you will need to tweak the code to suit your requirement. you will also need to check scenario where user uses back button of browser(from posted page) which will cause form to be posted again.


Stephen GregPosted Aug 3, 2021, 1:31 PM EDT
This is working fine for me. Additionally on the receiving page, I have added a client script in document.onready, but that is not working on IE, Chrome it is working.
Olas AwofadejuPosted Nov 1, 2020, 11:39 PM EST
Many thanks Jigar for this article. It was exactly what I was looking for and it worked very well after tweaking it to suit my need.
JAMAL MOHAMMEDPosted Sep 14, 2019, 2:45 AM EDT
It really helped me a perfectly. Very easy understanding and implementing. Thanks a lot for this article.
Pravash DashPosted Dec 26, 2017, 1:04 AM EST
Another question , if i want to post a table of data at a time. How is it possible?
Pravash DashPosted Dec 26, 2017, 1:03 AM EST
I want to store the response (xml response) after posting. How is it possible?
Pravash DashPosted Dec 26, 2017, 12:48 AM EST
How to get response or store the response after posting the form.
Ramesh PalaniappanPosted Aug 24, 2016, 6:21 AM EDT
Nice
kalu singh raoPosted Jul 9, 2016, 7:26 AM EDT
Nice...
rupesh gohilPosted Apr 11, 2015, 2:02 AM EDT
how to post data on web server database using ksoap2 web service in android
rajeev mishraPosted Sep 5, 2014, 9:20 PM EDT
please give me some idea about web scraping in asp.net
AppPosted Aug 21, 2012, 10:37 PM EDT
I want to use same functionality as you explained that for the test.aspx page which is having master page applied to it and form tag will be in master page only. Now I want to post the action for another page having name postpage.aspx and that postpage.aspx have master page assigned to it. I am trying to use your code but its not redirecting to the required postpage.aspx page from button submit on test.aspx page. Please help me..
Seminda RajapakshaPosted Aug 16, 2012, 11:40 AM EDT
thanks for the post. this solution solve my problem. great.....
anil babuPosted Jul 31, 2012, 11:58 PM EDT
What is URL REDIRECTION?How can i use this concept in my .net? my task is i have generated one url like this "http://example.com/" I am passing one pearameter like "http://example.com/Empno=1" I want to display Ename in Database table that corresponding "Empno" plz Help me Give me one simple example I am new this concepts plz Help me send source code to my mailID:[email protected]
Steve MundeneditedPosted Jan 27, 2012, 3:43 AM ESTEdited Jan 27, 2012, 3:45 AM EST
An old post, but still I found this very useful thanks. I suggest an addition to support those who do not have Javascript support or have it enabled, because of course otherwise they will just see a blank page... I apologise in advance if the syntax below is incorrect, as C is not my native language! ...Add after input loop and before closing /form tag.... /* ---Add message and submit button for non-Javascript users */ System.Web.HttpContext.Current.Response.Write("<p>If you see this message for more than a few seconds please click the button below to continue.</p>"); System.Web.HttpContext.Current.Response.Write("<input id=\"Submit1\" type=\"submit\" value=\"Continue\" />""); /* --- */
K V ReddyeditedPosted Nov 7, 2011, 8:39 AM ESTEdited Nov 7, 2011, 8:40 AM EST
your artile helped a lot, bunch of thanks.
Raju KatarePosted Oct 27, 2011, 10:58 PM EDT
Thanks
Laura KuhPosted Oct 25, 2011, 4:08 AM EDT
Exactly what I was looking for! Thanks so much!
An VoeditedPosted Jun 14, 2011, 3:11 AM EDTEdited Jun 14, 2011, 3:13 AM EDT
thanks
Brian HerbertPosted Mar 25, 2011, 2:30 AM EDT
This posting was very useful to me, there is only one snag. Where you have used string.Form, the html has been remove so that it reads simply "" for example System.Web.HttpContext.Current.Response.Write(""); should be System.Web.HttpContext.Current.Response.Write("<html><head>");
manish chopraPosted Feb 8, 2011, 7:02 PM EST
simple and beautiful.......... Thanks!
aaa aaaeditedPosted Dec 21, 2010, 7:36 PM ESTEdited Dec 21, 2010, 7:37 PM EST
Thanks very much, code worked perfectly and did exactly what I wanted it to do. Bruce
DeanPosted Aug 17, 2010, 9:57 AM EDT
Hi, I'm trying to get redirect via POST working with ASP.NET MVC. I'm using the code above as starting place, but I'm having trouble figuring out how to redirect to the URL instead of processing the result back. It's almost similar to the Redirect(url) command except it needs to accept a POST. Any ideas? Thanks, Dean
Athi NarayananPosted Aug 6, 2010, 2:40 AM EDT
This is very useful to me. Thanks!!!!
Ankur TandonPosted Aug 3, 2010, 10:51 PM EDT
Hi Jigar Desai, The code is working fine for me. But i need to put the code on receiving page. How to implement this without puttin the code on receiving page since i dont have any control over it. Thanks.
srk69Posted Jun 18, 2010, 11:59 AM EDT
Just what I was looking for!
sharonPosted Jun 8, 2010, 10:36 AM EDT
I need my website to post to another 3rd party website (which would be super if it's in a modal dialog), do some stuff there (it's not a webservice, it's a website on another domain which i don't control) and come back to my website without losing session. The other website will use post back to come back on my website. Is that feasable? I've been cracking my head open to find a solution to this for a week, any help would be greatly appreciated!! :)
Rich FryareditedPosted May 26, 2010, 1:11 AM EDTEdited May 26, 2010, 1:12 AM EDT
Clever idea. Easy to use. Well explained. Just a shame we have to resort to tricks like this in .NET. Rich
Bris CheiloPosted May 24, 2010, 7:05 AM EDT
Very good solution!!. Thanks :)
Paul LampingPosted May 12, 2010, 8:55 AM EDT
Thank you! I got it to work as a web user control within the SmartPart web part in SharePoint! Paul
MarkeditedPosted Apr 21, 2010, 12:47 AM EDTEdited Apr 21, 2010, 12:52 AM EDT
This was just what I needed, some times its difficult to see a simpler more elegant solution like this. Mark
Vivek JaggaPosted Apr 20, 2010, 11:05 PM EDT
Hi Jigar, I want to post some values (Title, Description, price etc.) from my local page to another web page of different website. the another website page has same fields (Title, Description, Price etc). I want to copy data deom my site to another site. I have and account on another website to post my data on server. What I will do?
Alex TimoshevskyPosted Feb 19, 2010, 5:26 AM EST
Hello. The solution quite good but have one hardship. The browser's "Back" button won't be work after redirection.
Onur BirinciPosted Oct 30, 2009, 8:13 PM EDT
Thank you Jigar. Your solution has been a great time saver. Instead of trying to figure out how to come up with a solution to this exact problem, I was able to use your solution with minor adjustments. Note to viewers: Download the code in the zip format. The displayed program logic has been stripped of necessary html code.
MaxencePosted Sep 30, 2009, 4:11 AM EDT
This class is very useful, thanks a lot. Warning: The good code is under the attached .zip
Ashish GandhiPosted Sep 17, 2009, 8:27 AM EDT
Ohh....Thats really great logic implemented with a great treak..so much impressive....Thanks..Thanks a lot....
jaime espinosaPosted Jul 13, 2009, 8:38 AM EDT
Simple, nice!!! Just a comment. The code shown is not right, so you have to download the code to get the correct version. Thanks a lot.-
carlos rodriguezPosted Jun 3, 2009, 12:31 PM EDT
i think is a great code. thanks.
michael campbellPosted May 20, 2009, 10:48 AM EDT
Hi, thanks for the code. I've converted it to vb.net, but i'm getting error Sys.WebForms.PageRequestManagerParserErrorException: The message received fro mthe server could not be parsed. Common causes for this error are when the sresponse is modofied by calls to Response.Write().respons filters, HpptModules, or server trace is enabled. Details : Error parsing near '<html><head></head><'. code 0 Url: http://localhost:1036/mysite.com/learnmore.aspx can anyone tell me how to fix this...i'm a bit of a noob. Public Class RemotePost Private Inputs As New System.Collections.Specialized.NameValueCollection() Public Url As String = "" Public Method As String = "post" Public FormName As String = "form1" Public Sub Add(ByVal name As String, ByVal value As String) Inputs.Add(name, value) End Sub Public Sub Post() System.Web.HttpContext.Current.Response.Clear() System.Web.HttpContext.Current.Response.Write("<html><head>") System.Web.HttpContext.Current.Response.Write(String.Format("</head><body onload=""document.{0}.submit()"">", FormName)) System.Web.HttpContext.Current.Response.Write(String.Format("<form name=""{0}"" method=""{1}"" action=""{2}"" >", FormName, Method, Url)) For i As Integer = 0 To Inputs.Keys.Count - 1 System.Web.HttpContext.Current.Response.Write(String.Format("<input name=""{0}"" type=""hidden"" value=""{1}"">", Inputs.Keys(i), Inputs(Inputs.Keys(i)))) Next System.Web.HttpContext.Current.Response.Write("</form>") System.Web.HttpContext.Current.Response.Write("</body></html>") System.Web.HttpContext.Current.Response.[End]() End Sub End Class
marcos maigreteditedPosted Mar 11, 2009, 8:32 PM EDTEdited Mar 11, 2009, 8:34 PM EDT
Después de mucho buscar encontré en tu artículo una brillante solución a cambiar la página de destino de un form usando C#. Muchas gracias por compartir. También necesité descargar el adjunto para conseguir que funcionara. Marcos
Jonny BravoPosted Mar 11, 2009, 2:28 PM EDT
This is exactly what I was looking for!
James HarrisoneditedPosted Feb 20, 2009, 2:26 AM ESTEdited Feb 20, 2009, 2:27 AM EST
Thank you very much for posting this article. I was in a bind and it was exactly what I needed. I had to integrate a new asp.net form with and old asp paypal processor page. This code worked perfect! Note: I did need to download the code to get it to work. The code in the article did not work for me. Thank you again for posting this! James www.alavri.com
Samir ShahPosted Feb 6, 2009, 9:13 AM EST
Hi Jigar, This article came to my viewing just when I needed it. I wanted to post back a form back to 2 different pages based on certain condition. One of the page is classic asp. This helps me achieve that. Thanks Samir
sathiya priyaPosted Oct 15, 2008, 10:02 PM EDT
This is working fine for me. But i got the exception "[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}" how can i restrict this one in my page Kindly help me on this Needs urgent Thanks Sathiya
Mark EvansPosted Sep 9, 2008, 7:37 AM EDT
hi this code helps a lot with my payment processing app... 2 things i want the form1 to submit automatically onload rather than by a button. how do i do this ? the action url is passed from the calling page... how do i use this in remotepost.aspx rather than a hard coded one ? it will pass in the querystring probably... thanks
Mark EvansPosted Sep 9, 2008, 7:36 AM EDT
hi this code helps a lot with my payment processing app... 2 things i want the form1 to submit automatically onload rather than by a button. how do i do this ? the action url is passed from the calling page... how do i use this in remotepost.aspx rather than a hard coded one ? it will pass in the querystring probably... thanks
Mark EvansPosted Sep 9, 2008, 7:36 AM EDT
hi this code helps a lot with my payment processing app... 2 things i want the form1 to submit automatically onload rather than by a button. how do i do this ? the action url is passed from the calling page... how do i use this in remotepost.aspx rather than a hard coded one ? it will pass in the querystring probably... thanks
Mark EvansPosted Sep 9, 2008, 7:36 AM EDT
hi this code helps a lot with my payment processing app... 2 things i want the form1 to submit automatically onload rather than by a button. how do i do this ? the action url is passed from the calling page... how do i use this in remotepost.aspx rather than a hard coded one ? it will pass in the querystring probably... thanks
JimPosted Mar 21, 2008, 7:51 AM EDT
Is there any way to get the back button to go to the original page, ie the page before the 'generated' form submit page?
sree_sundaramPosted Mar 14, 2008, 11:20 AM EDT
I am trying to get it to go to Paypal site. I have replicated the code exactly as is. However, the page does not navigate to Paypal site. Instead, it posts back to itself. I have tried setting the action property to an empty string in the form tog but it puts back to its own page (the default in ASP.NET 2.0). Any ideas will be appreciated.
Alex NevazhnoPosted Dec 11, 2007, 10:06 AM EST
Nice trick...it really helped me
Daniel MedranoPosted Nov 5, 2007, 11:25 AM EST
Hi your class is funcion correctly but i have a problem when open the pop up page, because the parent page is has been clear, how do i to avoid? or load page in the same page?... best regards.
Daniel MedranoPosted Nov 5, 2007, 11:11 AM EST
Hi your class is funcion correctly but i have a problem when open the pop up page, because the parent page is has been clear, how do i to avoid? or load page in the same page?... best regards.
EricPosted Oct 28, 2007, 8:44 AM EDT
How would i go about using this example if the form i want to submit is conntained in a content palceholder page that is attached to an asp.net master page?
venkatPosted Oct 18, 2007, 3:33 AM EDT
i developed one eshopping site . can i use this process to connect to sites like yourpay.com,paypal.com. How can i pass some of my details to those sites.. give me some examples how can i pass my values to yourpay.com.
Carlos LopezPosted Sep 6, 2007, 12:34 PM EDT
This is just exactly what i needed, damn, i had like 5 days breaking my head looking for this exactly thing, thank you very much, worked like a wonder once i found out what to put on those empty strings, xD
Ian JonesPosted Sep 3, 2007, 6:43 AM EDT
Hi, One criticism I have is that if the browser does not support JavaScript, the page will not be posted. IJ
fabrizio ciprianiPosted Jun 20, 2007, 1:47 AM EDT
simple and effective
Anders HundborgPosted May 16, 2007, 5:59 AM EDT
Clicking on zip-file leads to "empty" browser window. Right clicking and "Save target as.." leads to "No such interface supported". Vista problem / IE7 problem?
Dan BowPosted May 7, 2007, 5:34 AM EDT
just what i needed
Henrik LynnerupPosted May 1, 2007, 10:50 PM EDT
Hi, I have used your code and it works fine - I have one question though; I can't have it to open in a new window - like 'target="_blank"' - any help?
whoevereditedPosted Mar 14, 2007, 11:20 AM EDTEdited Mar 14, 2007, 11:25 AM EDT
Nice trick. Thanks
Robert BrownPosted Feb 20, 2007, 11:13 PM EST
I think the string.Format commands should have HTML instead of the empty strings. I wonder if it's a problem on this website that interpretted the HTML tags as actual tags instead of displaying them in the page. I tried to view source on the page but I can't see any tags so they must have been rendered out previously.
martin kjaerPosted Apr 29, 2006, 3:26 AM EDT
Hi desaijm, Im trying to use you code, but unfortunately it is not working. Without knowing anything about the System.Web.HttpContext.Current.Response class, I have been debugging your code. And it is obvious to me, that something is wrong in the post() method. System.Web.HttpContext.Current.Response.Write(string.Format("",FormName)); doesn't write anything at all, even though FormName has a value. I replaced the line with string a=(string.Format("",FormName)); And the string a is EMPTY. Dont know anything about the string.format method either, but i know that sending nothing DOES nothing.