1 ///");29 //Build the JavaScript which will do the Posting operation.30 StringBuilder strScript = new StringBuilder();31 strScript.Append("");36 //Return the form and the script concatenated.37 //(The order is important, Form then JavaScript)38 return strForm.ToString() + strScript.ToString();39 }2 /// This method prepares an Html form which holds all data 3 /// in hidden field in the addetion to form submitting script. 4 /// 5 /// The destination Url to which the post and redirection 6 /// will occur, the Url can be in the same App or ouside the App. 7 /// A collection of data that 8 /// will be posted to the destination Url. 9 ///Returns a string representation of the Posting form. 10 ///Samer Abu Rabie 11 12 private static String PreparePOSTForm(string url, NameValueCollection data)13 {14 //Set a name for the form15 string formID = "PostForm";16 //Build the form using the specified data to be posted.17 StringBuilder strForm = new StringBuilder();18 strForm.Append("
对于每一个想提交的数据, 我们创建了一个隐藏域来保存它的值,我们添加了必要的脚本,通过 vPostForm.submit() 来使表单完成自动提交操作。
1 ///2 /// POST data and Redirect to the specified url using the specified page. 3 /// 4 /// The page which will be the referrer page. 5 /// The destination Url to which 6 /// the post and redirection is occuring. 7 /// The data should be posted. 8 ///Samer Abu Rabie 9 10 public static void RedirectAndPOST(Page page, string destinationUrl, 11 NameValueCollection data)12 {13 //Prepare the Posting form14 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中运用