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();
- }
- }

Stephen GregPosted Aug 3, 2021, 5:31 PM
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 2, 2020, 4:39 AM
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, 6:45 AM
It really helped me a perfectly. Very easy understanding and implementing. Thanks a lot for this article.
Pravash DashPosted Dec 26, 2017, 6:04 AM
Another question , if i want to post a table of data at a time. How is it possible?
Pravash DashPosted Dec 26, 2017, 6:03 AM
I want to store the response (xml response) after posting. How is it possible?
Pravash DashPosted Dec 26, 2017, 5:48 AM
How to get response or store the response after posting the form.
Ramesh PalaniappanPosted Aug 24, 2016, 10:21 AM
Nice
kalu singh raoPosted Jul 9, 2016, 11:26 AM
Nice...
rupesh gohilPosted Apr 11, 2015, 6:02 AM
how to post data on web server database using ksoap2 web service in android
rajeev mishraPosted Sep 6, 2014, 1:20 AM
please give me some idea about web scraping in asp.net
AppPosted Aug 22, 2012, 2:37 AM
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, 3:40 PM
thanks for the post. this solution solve my problem. great.....
anil babuPosted Aug 1, 2012, 3:58 AM
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, 8:43 AMEdited Jan 27, 2012, 8:45 AM
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, 1:39 PMEdited Nov 7, 2011, 1:40 PM
your artile helped a lot, bunch of thanks.
Raju KatarePosted Oct 28, 2011, 2:58 AM
Thanks
Laura KuhPosted Oct 25, 2011, 8:08 AM
Exactly what I was looking for! Thanks so much!
An VoeditedPosted Jun 14, 2011, 7:11 AMEdited Jun 14, 2011, 7:13 AM
thanks
Brian HerbertPosted Mar 25, 2011, 6:30 AM
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 9, 2011, 12:02 AM
simple and beautiful.......... Thanks!
aaa aaaeditedPosted Dec 22, 2010, 12:36 AMEdited Dec 22, 2010, 12:37 AM
Thanks very much, code worked perfectly and did exactly what I wanted it to do. Bruce
DeanPosted Aug 17, 2010, 1:57 PM
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, 6:40 AM
This is very useful to me. Thanks!!!!
Ankur TandonPosted Aug 4, 2010, 2:51 AM
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, 3:59 PM
Just what I was looking for!
sharonPosted Jun 8, 2010, 2:36 PM
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, 5:11 AMEdited May 26, 2010, 5:12 AM
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, 11:05 AM
Very good solution!!. Thanks :)
Paul LampingPosted May 12, 2010, 12:55 PM
Thank you! I got it to work as a web user control within the SmartPart web part in SharePoint! Paul
MarkeditedPosted Apr 21, 2010, 4:47 AMEdited Apr 21, 2010, 4:52 AM
This was just what I needed, some times its difficult to see a simpler more elegant solution like this. Mark
Vivek JaggaPosted Apr 21, 2010, 3:05 AM
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, 10:26 AM
Hello. The solution quite good but have one hardship. The browser's "Back" button won't be work after redirection.
Onur BirinciPosted Oct 31, 2009, 12:13 AM
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, 8:11 AM
This class is very useful, thanks a lot. Warning: The good code is under the attached .zip
Ashish GandhiPosted Sep 17, 2009, 12:27 PM
Ohh....Thats really great logic implemented with a great treak..so much impressive....Thanks..Thanks a lot....
jaime espinosaPosted Jul 13, 2009, 12:38 PM
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, 4:31 PM
i think is a great code. thanks.
michael campbellPosted May 20, 2009, 2:48 PM
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 12, 2009, 12:32 AMEdited Mar 12, 2009, 12:34 AM
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, 6:28 PM
This is exactly what I was looking for!
James HarrisoneditedPosted Feb 20, 2009, 7:26 AMEdited Feb 20, 2009, 7:27 AM
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, 2:13 PM
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 16, 2008, 2:02 AM
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, 11:37 AM
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, 11:36 AM
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, 11:36 AM
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, 11:36 AM
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, 11:51 AM
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, 3:20 PM
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, 3:06 PM
Nice trick...it really helped me
Daniel MedranoPosted Nov 5, 2007, 4:25 PM
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, 4:11 PM
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, 12:44 PM
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, 7:33 AM
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, 4:34 PM
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, 10:43 AM
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, 5:47 AM
simple and effective
Anders HundborgPosted May 16, 2007, 9:59 AM
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, 9:34 AM
just what i needed
Henrik LynnerupPosted May 2, 2007, 2:50 AM
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, 3:20 PMEdited Mar 14, 2007, 3:25 PM
Nice trick. Thanks
Robert BrownPosted Feb 21, 2007, 4:13 AM
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, 7:26 AM
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.