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数据并跳转页面的更多相关文章
- ASP.NET 中 POST 数据并跳转页面(译自 Redirect and POST in ASP.NET)
本文翻译自 Samer Abu Rabie 的 <Redirect and POST in ASP.NET> 简介 在实际项目中,我们会遇到这样一种应用场景:我们需要与第三方 ...
- 在 ASP.NET 中创建数据访问和业务逻辑层(转)
.NET Framework 4 当在 ASP.NET 中处理数据时,可从使用通用软件模式中受益.其中一种模式是将数据访问代码与控制数据访问或提供其他业务规则的业务逻辑代码分开.在此模式中,这两个层均 ...
- asp.net中几个网页跳转的方法及区别
1:注意:Response.Redirect("a.html")是不能跳出框架.IFRAME的. 可以使用 Response.Write("<script Lang ...
- ASP.NET中后台数据和前台控件的绑定
关于ASP.NET中后台数据库和前台的数据控件的绑定问题 最近一直在学习个知识点,自己创建了SQL Server数据库表,想在ASP.NET中连接数据库,并把数据库中的数据显示在前台,注意,这里的数据 ...
- ASP.NET中数据库数据导入Excel并打印(2)
大家可能觉得上面的代码比较复杂,因为上面对于对打印要求比较高的应用,是十分有效的.如果只是单单对数据进行导出,还可以使用简单的格式,比如使用以下的代码: Private Sub Page_L ...
- 在小程序中修改上一个页面里data中的数据调用上一个页面的方法
//获取已经打开的页面的数组 var pages = getCurrentPages(); //获取上一个页面的所有的方法和data中的数据 var lastpage = pages[pages.l ...
- ASP.NET中POST提交数据并跳转页面
需求:先Post提交数据,然后跳转到目标页面 找了好久才发现这个神奇的类HttpHelper.原理很简单,利用html的from表单拼接,然后执行 使用方法: NameValueCollection ...
- ASP.NET中GridView数据导出到Excel
/// <summary> /// 导出按钮 /// </summary> /// <param name="sender"></para ...
- asp.net 中插入数据到access
这样报语法错误: insert into Menu_wx(userid,menutype,MenuID,menuname,MenuTitle,Url,Key,OrderValue,State) val ...
随机推荐
- linux下 cmatrix的安装和使用
安装过程 wget http://www.asty.org/cmatrix/dist/cmatrix-1.2a.tar.gztar xvf cmatrix-1.2a.tar.gzcd cmatrix- ...
- 【组合数的唯一分解定理】Uva1635
给出n.m,求得最终求和数列an=C(n-1,0)*x1 + C(n-1,1)*x2+...+C(n-1,n-1)*xn; 若xi与m无关,则an除以m的余数与xi无关,即余数不含xi的项: 输入:n ...
- 用bat使用date和time命令
D:\>date /T 2010-12-10 星期五 D:\>echo %date:~0,10% 2010-12-10 date:命令(别忘记date后面有个冒号) ~0:从索引0开始取内 ...
- hdu 2709 Sumsets
Sumsets Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- Jersey(1.19.1) - Client API, Testing services
The Jersey client API was originally developed to aid the testing of the Jersey server-side, primari ...
- MyBatis(3.2.3) - Multiple results as a map
If we have a mapped statement that returns multiple rows and we want the results in a HashMap with s ...
- Ehcache(2.9.x) - API Developer Guide, Searching a Cache
About Searching The Search API allows you to execute arbitrarily complex queries against caches. The ...
- Git CMD - rm: Remove files from the working tree and from the index
命令格式 git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>… 命令参 ...
- Javascript中while和do-while循环用法详解
while循环 while 语句与 if 语句相似,都有条件来控制语句(或语句块)的执行,其语言结构基本相同:while(conditions){ statements;} while 语句与 ...
- ORA-00001: unique constraint (...) violated解决方案
ORA-00001: unique constraint (...) violated 的解决方案 今天往Oracle数据库里插入数据一条记录的时候,报错了, 控制台抛出异常:违反唯一性约定, 我以为 ...