ASP.NET中POST数据并跳转页面
2016-06-27 17:27
681 查看
需求:先Post提交数据,然后跳转到目标页面
找了好久才发现这个神奇的类HttpHelper。原理很简单,利用html的from表单拼接,然后执行
使用方法:
HttpHelper类:
找了好久才发现这个神奇的类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_Ajax实训练习之创建和使用Ajax组件
- ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合
- ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节
- Asp.Net MVC4 系列-- 进阶篇之路由(1)
- asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码实例
- ASP.NET 实现人民币大写转换函数
- asp.net webform中使用async,await实现异步操作
- ASP.NET MVC 实现二级域名
- asp.net form 验证方式的使用(转载)
- asp.net下通过泛解析和伪静态实现二级域名的实现方法
- ASP.NET MVC AJAX的调用示例
- ASP.NET Session 详解
- WebForm页面生命周期及asp.net运行机制
- .NET/ASP.NET Routing路由(深入解析路由系统架构原理)
- ASP 连接Access2013的accdb文件
- spring AspectJ的Execution表达式-备忘札记
- JDK8:PermGen变更为MetaSpace详解
- ASPNET常用工具方法(二)
- 利用ASP.NET MVC+Bootstrap搭建个人博客之打造清新分页Helper(三)
- [转]asp.net c# 网上搜集面试题目(附答案)