Authordate of Submissionuser Leveljigar desai09/27/2004intermediate
Download:remote Post 2Kb Introductionsometime your need to post a form of different URL from asp.net pages, for example You are might need to send the user to the third party payment processing system using POST method, ASP.net does not provide any stra Ight forward way to accomplish the this task.
Problem which most users faces with server side form in aspx page are, for you are don't allowed to change action of form and yo U are allowed to use only one server side form per page.
Possible Solutions
1) One possible solution to this problem be to Create your own form control and use it in page this would allow to Chan GE action of form, but again what if you don't want some existing input elements in the current page to the to post.
2) There is good way to post form data using HttpWebResponse & HttpWebRequest class If you want to post data behind th E scenes, but if you want to post the data using user browser then you are stuck.
Our Solution
I'll try to show your one possible way to accomplish this task, we'll create 1 component that'll create form with req uired fields and post the form to specified URLs, 2 Web page that would use that component to post data and 3) page which W Ill 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 (String. Format ("", FormName))
System.Web.HttpContext.Current.Response.Write (String. Format ("",
Formname,method,url))
for (int i=0i< inputs.keys.counti++) {
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 ()
}
}
Properties of our Component
1 "URL" which is action's our form.
2 "Method" which are method by our form, the default is Post but you can also use
3 "FormName" which is name of form.
Methods of our component.
1 "ADD" which would be used to add form input name and value. and
2) "Post" which'll render HTML on page to do actual posting, most important the part of the is onload event of Rende Red HTML ' s body which'll post form to specified URL.
and private field inputs which'll hold name value pair collection of all inputs to the into form.
Can compile this class to DLL and use in your project but for simplicity I am including the 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 ()
C) receiving Page.
Following is sample page code which posts form to specified URL.
This is the page where posting'll occur for simplicity we'll just write posed value so, we can know what was Poste D. <%@ 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>
Run Sample
Click "Http://www.jigar.net/demo/RemotePost.aspx" target= "new" >here to run sample
There is cases where you'll need to tweak the code to suit your. You'll also need to check scenario where user uses back button of browser (from posted page) which'll cause form to IS Posted again.
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 (String. Format ("", FormName))
System.Web.HttpContext.Current.Response.Write (String. Format ("",
Formname,method,url))
for (int i=0i< inputs.keys.counti++) {
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 ()
}
}
Properties of our Component
1 "URL" which is action's our form.
2 "Method" which are method by our form, the default is Post but you can also use
3 "FormName" which is name of form.
Methods of our component.
1 "ADD" which would be used to add form input name and value. and
2) "Post" which'll render HTML on page to do actual posting, most important the part of the is onload event of Rende Red HTML ' s body which'll post form to specified URL.
and private field inputs which'll hold name value pair collection of all inputs to the into form.
Can compile this class to DLL and use in your project but for simplicity I am including the 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 ()
C) receiving Page.
Following is sample page code which posts form to specified URL.
This is the page where posting'll occur for simplicity we'll just write posed value so, we can know what was Poste D. <%@ 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>
Run Sample
Click "Http://www.jigar.net/demo/RemotePost.aspx" target= "new" >here to run sample
There is cases where you'll need to tweak the code to suit your. You'll also need to check scenario where user uses back button of browser (from posted page) which'll cause form to IS Posted again. Jigar Desai
Jigar Desai is asp.net and. Net consultant and the. Looking for good assignment in USA.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.