国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Posting form data from ASP.NET page to another URL

2024-07-10 12:56:20
字體:
來源:轉載
供稿:網友


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 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()
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 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()
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.
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 株洲县| 西城区| 枞阳县| 北流市| 霍山县| 城固县| 中江县| 杭锦后旗| 梁河县| 泸州市| 盐城市| 南陵县| 竹山县| 阳东县| 永寿县| 衡东县| 汉沽区| 寿光市| 获嘉县| 漯河市| 锦州市| 道真| 平武县| 沅江市| 罗山县| 金坛市| 商洛市| 同江市| 通江县| 桐庐县| 汾西县| 嘉定区| 来宾市| 石门县| 苏尼特右旗| 平湖市| 林州市| 沙坪坝区| 南城县| 丁青县| 巩留县|