ASP.NET中POST数据并跳转页面

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET中POST数据并跳转页面相关的知识,希望对你有一定的参考价值。

需求:先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数据并跳转页面的主要内容,如果未能解决你的问题,请参考以下文章

PHP 用CURL POST后获取cookis并跳转到另一个页面

JS解析Json 数据并跳转到一个新页面,取消A 标签跳转

JavaScript解决一个带验证的Form两个Submit事件(一个页面保持不动AJAX实现,一个页面提交并跳转)的场景

asp.net如何把表单提交到别的页面而不是本页面,用POST方式

asp.NET Jquery post请求回调函数的问题

asp.net在同一页面跳转到指定位置