Asp.net中的页面跳转及post数据

 1 /// <summary>
 2 /// This method prepares an Html form which holds all data
 3 /// in hidden field in the addetion to form submitting script.
 4 /// </summary>
 5 /// <param name="url">The destination Url to which the post and redirection
 6 /// will occur, the Url can be in the same App or ouside the App.</param>
 7 /// <param name="data">A collection of data that
 8 /// will be posted to the destination Url.</param>
 9 /// <returns>Returns a string representation of the Posting form.</returns>
10 /// <Author>Samer Abu Rabie</Author>
11  
12 private static String PreparePOSTForm(string url, NameValueCollection data)
13 {
14     //Set a name for the form
15     string formID = "PostForm";
16     //Build the form using the specified data to be posted.
17     StringBuilder strForm = new StringBuilder();
18     strForm.Append("<form id=\"" + formID + "\" name=\"" + 
19                    formID + "\" action=\"" + url + 
20                    "\" method=\"POST\">");
21  
22     foreach (string key in data)
23     {
24         strForm.Append("<input type=\"hidden\" name=\"" + key + 
25                        "\" value=\"" + data[key] + "\">");
26     }
27  
28     strForm.Append("</form>");
29     //Build the JavaScript which will do the Posting operation.
30     StringBuilder strScript = new StringBuilder();
31     strScript.Append("<script language="'javascript'">");
32     strScript.Append("var v" + formID + " = document." + 
33                      formID + ";");
34     strScript.Append("v" + formID + ".submit();");
35     strScript.Append("</script>");
36     //Return the form and the script concatenated.
37     //(The order is important, Form then JavaScript)
38     return strForm.ToString() + strScript.ToString();
39 }

  对于每一个想提交的数据, 我们创建了一个隐藏域来保存它的值,我们添加了必要的脚本,通过 vPostForm.submit() 来使表单完成自动提交操作。

 

 1 /// <summary>
 2 /// POST data and Redirect to the specified url using the specified page.
 3 /// </summary>
 4 /// <param name="page">The page which will be the referrer page.</param>
 5 /// <param name="destinationUrl">The destination Url to which
 6 /// the post and redirection is occuring.</param>
 7 /// <param name="data">The data should be posted.</param>
 8 /// <Author>Samer Abu Rabie</Author>
 9  
10 public static void RedirectAndPOST(Page page, string destinationUrl, 
11                                    NameValueCollection data)
12 {
13 //Prepare the Posting form
14 string strForm = PreparePOSTForm(destinationUrl, data);
15 //Add a literal control the specified page holding 
16 //the Post Form, this is to submit the Posting form with the request.
17 page.Controls.Add(new LiteralControl(strForm));
18 }

 

1 NameValueCollection data = new NameValueCollection();
2 data.Add("v1", "val1");
3 data.Add("v2", "val2");
4 HttpHelper.RedirectAndPOST(this.Page, "http://DestUrl/Default.aspx", data);

同样的这种思路还可以在js中运用

 

转载于:https://www.cnblogs.com/meiCode/p/4929878.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值