• 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中运用

  • 相关阅读:
    DRY原则和Shy原则
    GEO LBS服务
    Intellij IDEA转换类图、时序图
    使用HttpMessageConverter实现HTTP的序列化和反序列化
    通过@Valid注解对请求主体中的参数进行校验
    Java-Enumeration ( 枚举类)
    SPI的实现原理
    python批量获取gitlab里面所有项目的代码
    浙江省高等学校教师教育理论培训上机考试小程序设计架构
    springboot~使用freemaker模版进行部署
  • 原文地址:https://www.cnblogs.com/meiCode/p/4929878.html
Copyright © 2020-2023  润新知