ASP.NET中POST提交資料並跳轉頁面

來源:互聯網
上載者:User

標籤:

需求:先Post提交資料,然後跳轉到目標頁面

找了好久才發現這個神奇的類HttpHelper。原理很簡單,利用html的from表單拼接,然後執行

使用方法:

     NameValueCollection data = new NameValueCollection();        data.Add("v1", "val1");        data.Add("v2", "val2");        HttpHelper.RedirectAndPOST(this.Page, "2.aspx", data);

 

HttpHelper類:

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Net;using System.Collections.Specialized;using System.Text;/// <summary>/// Summary description for HttpHelper/// </summary>/// <Author>Samer Abu Rabie</Author>public static class HttpHelper{ /// <summary> /// This method prepares an Html form which holds all data in hidden field in the addetion to form submitting script. /// </summary> /// <param name="url">The destination Url to which the post and redirection will occur, the Url can be in the same App or ouside the App.</param> /// <param name="data">A collection of data that will be posted to the destination Url.</param> /// <returns>Returns a string representation of the Posting form.</returns> /// <Author>Samer Abu Rabie</Author> private static String PreparePOSTForm(string url, NameValueCollection data) { //Set a name for the form string formID = "PostForm"; //Build the form using the specified data to be posted. StringBuilder strForm = new StringBuilder(); strForm.Append("<form id=\"" + formID + "\" name=\"" + formID + "\" action=\"" + url + "\" method=\"POST\">"); foreach (string key in data) { strForm.Append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key] + "\">"); } strForm.Append("</form>"); //Build the JavaScript which will do the Posting operation. StringBuilder strScript = new StringBuilder(); strScript.Append("<script language=‘javascript‘>"); strScript.Append("var v" + formID + " = document." + formID + ";"); strScript.Append("v" + formID + ".submit();"); strScript.Append("</script>"); //Return the form and the script concatenated. (The order is important, Form then JavaScript) return strForm.ToString() + strScript.ToString(); } /// <summary> /// POST data and Redirect to the specified url using the specified page. /// </summary> /// <param name="page">The page which will be the referrer page.</param> /// <param name="destinationUrl">The destination Url to which the post and redirection is occuring.</param> /// <param name="data">The data should be posted.</param> /// <Author>Samer Abu Rabie</Author> public static void RedirectAndPOST(Page page, string destinationUrl, NameValueCollection data) { //Prepare the Posting form string strForm = PreparePOSTForm(destinationUrl, data); //Add a literal control the specified page holding the Post Form, this is to submit the Posting form with the request. page.Controls.Add(new LiteralControl(strForm)); }}

 

ASP.NET中POST提交資料並跳轉頁面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.