Asp.net中的页面跳转及post数据
2015-11-02 13:09
441 查看
/// <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(); }
对于每一个想提交的数据, 我们创建了一个隐藏域来保存它的值,我们添加了必要的脚本,通过
vPostForm.submit() 来使表单完成自动提交操作。
/// <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)); }
NameValueCollection data = new NameValueCollection(); data.Add("v1", "val1"); data.Add("v2", "val2"); HttpHelper.RedirectAndPOST(this.Page, "http://DestUrl/Default.aspx", data);
同样的这种思路还可以在js中运用
相关文章推荐
- CS1061: “ASP.send_aspx”不包含“sender_TextChanged”的定义,并且找不到可接受类型为“ASP.send_aspx”的第一个参数的扩展方法“sender_Text
- 玩转树莓派--树莓派准备工作
- asp.net报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”的解决办法
- asp.net报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”的解决办法
- asp.net中“从客户端中检测到有潜在危险的Request.Form值”错误的解决办法
- ASP.NET如何使用web服务的会话状态
- asp.net 错误提示:Could not load type 'SendEmail.WebPage'
- asp.net 页面上的点击事件
- asp.net:repeater嵌套(常用于新闻等在首页归类显示)
- ASP.Net在web.config中设置上传文件的大小方法
- ASP.Net中的四种状态保持机制
- 深入理解asp.net SessionState
- ASP.NET之Gridview图解
- asp.net微信公众平台开发的技术研究
- ASP.Net传参汇总
- asp.net之简易聊天室
- asp.net中gridview的checkbox使用总结
- 16、ASP.NET MVC入门到精通——MVC过滤器
- ASP.NET WebAPI 07 路由
- ASP.NET Web API基于OData的增删改查,以及处理实体间关系