欢迎光临
我们一直在努力

Posting form data from ASP.NET page to another URL-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

source:

http://www.c-sharpcorner.com/code/2004/sept/asp.netposturl.asp

begin:

authordate of submissionuser leveljigar desai09/27/2004intermediate
download: remote post 2kb introductionsometime you need to post a form to an different url 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
1) 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.

2) 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 1)component that will create form with required fields and post the form to specified url, 2) web page that will use that component to post data and 3) 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=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 of our form.

2) “method” which is method of our form, default is post but you can also use get

3) “formname” which is name of form.

methods of our component.

1) “add” which will be used to add form input name and value. and

2) “post” which will render html on page to do actual posting, most important part of this method is onload event of rendered htmls 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()
c) receiving page.

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>
run sample

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.
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=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 of our form.

2) “method” which is method of our form, default is post but you can also use get

3) “formname” which is name of form.

methods of our component.

1) “add” which will be used to add form input name and value. and

2) “post” which will render html on page to do actual posting, most important part of this method is onload event of rendered htmls 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()
c) receiving page.

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>
run sample

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.jigar desai
jigar desai is asp.net and .net consultant and he is looking for good assignment in usa.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Posting form data from ASP.NET page to another URL-.NET教程,Asp.Net开发
分享到: 更多 (0)