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中http提交数据所遇到的那些坑
http提交数据有两种形式,get和post,不知道的同学请联系度娘. 1.aspnet:MaxHttpCollectionKeys 业务场景:业务很简单,手机端读取本地通讯录,将所有通讯录提交到后台 ...
- 解决js中post提交数据并且跳转到指定页面的问题总结
今天在开发中过程中遇到了这个问题,js中利用JQuery中的 $.post("url", id, function(){}); 这个方法是数据提交正常,但是后台处理完成之后跳转无法 ...
- ThinkPHP中ajax提交数据
最近在做项目时遇到了一些需要从页面用ajax提交数据到后台的操作,无奈本人技术有限,网上苦寻,研究了一下ajax和thinkPHP的结合,黄天不负苦心人,终于搞定了. 闲话少叙,进入正题:我需要从页面 ...
- ASP.NET 中 POST 数据并跳转页面(译自 Redirect and POST in ASP.NET)
本文翻译自 Samer Abu Rabie 的 <Redirect and POST in ASP.NET> 简介 在实际项目中,我们会遇到这样一种应用场景:我们需要与第三方 ...
- ASP.NET中POST数据并跳转页面
需求:先Post提交数据,然后跳转到目标页面 找了好久才发现这个神奇的类HttpHelper.原理很简单,利用html的from表单拼接,然后执行 使用方法: NameValueCollection ...
- form 表单提交数据 不跳转解决办法
1. 利用隐藏的 iframe —— 只需form的 target 指向iframe的name:可不用form 的action默认提交,自己写ajax 提交数据. <html> < ...
- Django中Ajax提交数据的CSRF问题
错误信息: Forbidden (CSRF token missing or incorrect.): 什么是CSRF: django为用户实现防止跨站请求伪造的功能,通过中间件 django.mid ...
- js通过生成临时表单再删除的方式向后台提交数据(模拟ajax的post提交但还要跳转页面不返回数据)以及 struts向前台返回文件下载及防止中文乱码处理
为了避免发送数据中有特殊字符,发送时用 encodeURIComponent 编码 (其实这个 if中是直接通过浏览器下载文件的方法,else是向后台传数据的方法) struts后台Action处理接 ...
- 通过jquery实现form表单提交后不跳转页面,保留当前页面
jquery代码: <script type="text/javascript" src="../js/jquery-1.8.3.min.js">& ...
随机推荐
- 1486: [HNOI2009]最小圈 - BZOJ
在机房的小伙伴提醒是二分之后,我想到了是判负环,所以我用spfa,而且我保持dis都是小于等于0,本以为这样就能过了,可是还是有一个点达到了3.8s左右(其他都是0.0几秒) 所以还是写了dfs版 ...
- Ext学习-基础组件介绍
1.目标 学习对象获取,组件基础,事件模型以及学习ExtJS中的基础组件的应用. 2.内容 1.对象获取 2.组件原理以及基础 3.事件模型 4.常用组件的介绍 3.学习步骤 1 ...
- 【转载】Spring加载resource时classpath*:与classpath:的区别
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:kyfxbl 原文地址: spring配置中classpath和cla ...
- 解决Maven默认仓库没有的jar下载(二)
前言: 在 “解决Maven不能下载“oracle.aspectjweaver.com.springsource.net.sf.cglib”jar(http://www.cnblogs.com/wql ...
- C++代码反汇编后的堆栈寄存器EBP和ESP
最近在分析一个进程崩溃的严重问题,其中有些过程分析需要对ebp, esp 有清晰的理解,对于ebp 和esp 相信大家都很熟悉了,但是为了使本文自成体系,我还是解释一下. ebp--栈底指针 esp- ...
- 《head first java 》读书笔记(二)
Updated 2014/03/27 P402-P454 Updated 2014/04/03 P454- 世界三大首席管理器: border, flow, box borderLayout: 五个区 ...
- [转载]Spring Beans Auto-Wiring
Autowiring Modes You have learnt how to declare beans using the <bean> element and inject < ...
- poj 1562 Oil Deposits (广搜,简单)
题目 简单的题目,只是测试案例的输入后面可能有空格,所以要注意一下输入方式. #define _CRT_SECURE_NO_WARNINGS //题目的案例输入n,m后面有些貌似有空格... #inc ...
- Nodejs常见安装
1.Windows 下成功安装node-canvas2.jsdom——node.js的DOM
- POJ1573Robot Motion
http://poj.org/problem?id=1573 #include<stdio.h> #include<stdlib.h> #include<cstring& ...