webform 页面传值的方法总结
ASP.NET页面之间传递值的几种方式
页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值、存储对象传值、ajax、类、model、表单等。但是一般来说,常用的较简单有QueryString,Session,Cookies,Application,Server.Transfer。
一、QueryString
QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中。如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法。但是对于传递数组或对象的话,就不能用这个方法了。
这种方法的优点:1.使用简单,对于安全性要求不高时传递数字或是文本值非常有效。
这种方法的缺点:1.缺乏安全性,由于它的值暴露在浏览器的URL地址中的。
2.不能传递对象。
使用方法:1.在源页面的代码中用需要传递的名称和值构造URL地址。
2.在源页面的代码用Response.Redirect(URL);重定向到上面的URL地址中。
3.在目的页面的代码使用Request.QueryString["name"];取出URL地址中传递的值。
例子:(1)a.aspx
private void Button1_Click(object sender, System.EventArgs e)
{
string s_url;
s_url = "b.aspx?name=" + Label1.Text;
Response.Redirect(s_url);
}
(2)b.aspx
private void Page_Load(object sender, EventArgs e)
{
Label2.Text = Request.QueryString["name"];
}
二、Session
想必这个肯定是大家使用中最常见的用法了,其操作与Application类似,作用于用户个人,所以,过量的存储会导致服务器内存资源的耗尽。
优点:1.使用简单,不仅能传递简单数据类型,还能传递对象。
2.数据量大小是不限制的。
缺点:1.在Session变量存储大量的数据会消耗较多的服务器资源。
2.容易丢失。
使用方法:1.在源页面的代码中创建你需要传递的名称和值构造Session变量:Session["Name"]="Value(Or Object)";
2.在目的页面的代码使用Session变量取出传递的值。Result = Session["Nmae"]
注意:session不用时可以销毁它,销毁的方法是:清除一个:Session.Remove("session名");
清除所有:Session.Clear();
例子:(1)a.aspx
private void Button1_Click(object sender, System.EventArgs e)
{
Session["name"] = Label.Text;
}
(2)b.aspx
private void Page_Load(object sender, EventArgs e)
{
string name;
name = Session["name"].ToString();
}
三、Cookie
这个也是大家常使用的方法,Cookie用于在用户浏览器上存储小块的信息,保存用户的相关信息,比如用户访问某网站时用户的ID,用户的偏好等,用户下次访问就可以通过检索获得以前的信息。所以Cookie也可以在页面间传递值。Cookie通过HTTP头在浏览器和服务器之间来回传递的。Cookie只能包含字符串的值,如果想在Cookie存储整数值,那么需要先转换为字符串的形式。
与Session一样,其是什对每一个用户而言的,但是有个本质的区别,即Cookie是存放在客户端的,而session是存放在服务器端的。而且Cookie的使用要配合ASP.NET内置对象Request来使用。
优点:1.使用简单,是保持用户状态的一种非常常用的方法。比如在购物网站中用户跨多个页面表单时可以用它来保持用户状态。
缺点:1.常常被人认为用来收集用户隐私而遭到批评。
2.安全性不高,容易伪造。
使用方法:1.在源页面的代码中创建你需要传递的名称和值构造Cookie对象:
HttpCookie objCookie = new HttpCookie("myCookie","Hello,Cookie!");
Response.Cookies.Add(cookie);
2.在目的页面的代码使用Cookie对象取出传递的值:Result = Request.Cookies[ "myCookie" ].Value;
例子:(1)a.aspx
private void Button1_Click(object sender, System.EventArgs e)
{
HttpCookie objCookie = new HttpCookie("myCookie","Hello,Cookie!");
Response.Cookies.Add(objCookie);
}
(2)b.aspx
string myName1Value;
myName1Value = Request.Cookies[ "myCookie" ].Value;
四、Application
Application对象的作用范围是整个全局,也就是说对所有用户都有效。它在整个应用程序生命周期中都是有效的,类似于使用全局变量一样,所以可以在不同页面中对它进行存取。它和Session变量的区别在于,前者是所有的用户共用的全局变量,后者是各个用户独有的全局变量。
可能有人会问,既然所有用户都可以使用application变量,那他可以用在什么场合呢?这里举个例子:网站访问数。多个请求访问时都可以对它进行操作。
优点:1.使用简单,消耗较少的服务器资源。
2.不仅能传递简单数据,还能传递对象。
3.数据量大小是不限制的。
缺点:1.作为全局变量容易被误操作。所以单个用户使用的变量一般不能用application。
使用方法:1.在源页面的代码中创建你需要传递的名称和值构造Application变量:Application["Nmae"]="Value(Or Object)";
2.在目的页面的代码使用Application变量取出传递的值。Result = Application["Nmae"]
注意:常用lock和unlock方法用来锁定和解锁,为了防止并发修改。
例子:(1)a.aspx
private void Button1_Click(object sender, System.EventArgs e)
{
Application["name"] = Label1.Text;
}
(2)b.aspx

private void Page_Load(object sender, EventArgs e)
{
string name;
Application.Lock();
name = Application["name"].ToString();
Application.UnLock();
}

五、Server.Transfer
这个才可以说是面象对象开发所使用的方法,其使用Server.Transfer方法把流程从当前页面引导到另一个页面中,新的页面使用前一个页面的应答流,所以这个方法是完全面象对象的,简洁有效。
Server.Transfer是从当前的ASPX页面转到新的ASPX页面,服务器端执行新页并输出,在新页面中通过Context.Handler来获得前一个页面传递的各种数据类型的值、表单数据、QueryString.由于重定向完全在服务器端完成,所以客户端浏览器中的URL地址是不会改变的。调用Server.Transfer时,当前的ASPX页面终止执行,执行流程转入另一个ASPX页面,但新的ASPX页面仍使用前一ASPX页面创建的应答流。
ps:比较Server.Transfer和Response.Redirect的区别。
(1)Server.Transfer在服务器端完成,所以客户端浏览器中的URL地址是不会改变的;Response.Redirect是客户端完成,向服务器端提出新的页面处理请求,所以客户端浏览器中的URL地址是会改变的。
(2)Server.Transfer在服务器端完成,不需要客户端提出请求,减少了客户端对服务器端提出请求。[2]
(3)Server.Transfer只能够转跳到本地虚拟目录指定的页面,也就是工程项目中的页面,而Response.Redirect则十分灵活,可以跳转到任何URL地址。
(4)Server.Transfer可以将前一个页面的各种类型的值传到新的页面;Response.Redirect则只能借助URL中带参数或是结合上面四种办法把各种类型的值传到新的页面。
优点:1.直接在服务器端重定向,使用简单方便,减少了客户端对服务器端提出请求。
2.可以传递各种数据类型的值和控件的值。
缺点:1.客户端浏览器中的URL地址是不改变,会导致在新的页面可能出现一些意想不到的问题。比如如果源页面和目的页面不在同一个虚拟目录或其子目录下,那么使用相对路径的图片、超链接都会导致错误的指向。
使用方法:1.在源页面的代码中,使用Page类的Server.Transfer跳到另一个页面传递页面数据:Server.Transfer("b.aspx","false")。
2.在目的页面中,使用Context.Handler来接收数据:FormerPage formerPage = (FormerPage)Context.Handler; 然后用formerPage的属性和方法来获取前一个页面的值,或者直接用Context.Items["myParameter "]
例子:(1)a.aspx

public string Name
{
get{ return Label1.Text;}
}
private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("b.aspx");
}

(2)b.aspx

private void Page_Load(object sender, EventArgs e)
{
a newWeb; //实例a窗体
newWeb = (source)Context.Handler;
string name;
name = newWeb.Name;
}

以上就是常用的几种页面间传值的方法,我一般使用session和querystring来传值,少数情况会使用到cookie。本篇文章仅仅是介绍这几种方法的使用方法,内部原理没有过多的解释,关于session的存储方式请参见:session的存储方式和配置
参考文章:1、ASP.NET页面之间传递值的几种方式
接下来再补充几个。。。。
Each button in the demo has the required code in it's Click event handler, and brings you to a separate target page that gets and displays the data that was sent. Here are the methods:
1. Use the querystring:
protected void QueryStringButton_Click(object sender, EventArgs e)
{
Response.Redirect("QueryStringPage.aspx?Data=" + Server.UrlEncode(DataToSendTextBox.Text));
}
2. Use HTTP POST:
<asp:Button ID="HttpPostButton" runat="server" Text="Use HttpPost"
PostBackUrl="~/HttpPostPage.aspx" onclick="HttpPostButton_Click"/>
protected void HttpPostButton_Click(object sender, EventArgs e)
{
// The PostBackUrl property of the Button takes care of where to send it!
}
3. Use Session State:
protected void SessionStateButton_Click(object sender, EventArgs e)
{
Session["Data"] = DataToSendTextBox.Text;
Response.Redirect("SessionStatePage.aspx");
}
4. Use public properties:
public string DataToSend
{
get
{
return DataToSendTextBox.Text;
}
}
protected void PublicPropertiesButton_Click(object sender, EventArgs e)
{
Server.Transfer("PublicPropertiesPage.aspx");
}
5. Use PreviousPage Control Info:
protected void ControlInfoButton_Click(object sender, EventArgs e)
{
Server.Transfer("ControlInfoPage.aspx");
}
// target page:
protected void Page_Load(object sender, EventArgs e)
{
var textbox = PreviousPage.FindControl("DataToSendTextbox") as TextBox;
if (textbox != null)
{
DataReceivedLabel.Text = textbox.Text;
}
}
6. Use HttpContext Items Collection:
protected void HttpContextButton_Click(object sender, EventArgs e)
{
HttpContext.Current.Items["data"] = DataToSendTextBox.Text;
Server.Transfer("HttpContextItemsPage.aspx");
}
// target page:
protected void Page_Load(object sender, EventArgs e)
{
this.DataReceivedLabel.Text =(String) HttpContext.Current.Items["data"];
}
7. Use Cookies:
protected void CookiesButton_Click(object sender, EventArgs e)
{
HttpCookie cook = new HttpCookie("data");
cook.Expires = DateTime.Now.AddDays(1);
cook.Value = DataToSendTextBox.Text;
Response.Cookies.Add(cook);
Response.Redirect("HttpCookiePage.aspx");
}
// target page:
protected void Page_Load(object sender, EventArgs e)
{
DataReceivedLabel.Text = Request.Cookies["data"].Value;
}
8. Use Cache:
protected void CacheButton_Click(object sender, EventArgs e)
{
Cache["data"] = DataToSendTextBox.Text;
Server.Transfer("CachePage.aspx");
}
// target page:
protected void Page_Load(object sender, EventArgs e)
{
this.DataReceivedLabel.Text = (string) Cache["data"];
}
Actually, there are a number of other methods. You can use a database, a Data Store such as Redis or BPlusTree, file storage, or even the Appdomain Cache. But as these are not as common, we'll leave those as an exercise for the reader. I should mention that the use of Cache and Application (not shown) are not user - specific, but they can easily be made so by prepending the key used with something unique to the user such as their SessionId.
You can download the complete Visual Studio 2010 demo solution here.
webform 页面传值的方法总结的更多相关文章
- Struts2中在Action里面向前端页面传值的方法总结
由于在Action中并不能直接诶访问Servlet API,但它提供了相关类ActionContext来访问HttpServletRequest.HttpSession和ServletContext, ...
- Asp.net 页面传值的方法
ASP.NET页面传值的方法 From:Refresh-air 在面试的时候,经常会遇到这样的问题,其实我们会对其中的几种方法比较熟悉,因为项目中经常使用.但是要全面的回答ASP.NET中页面传值的方 ...
- ASP.NET页面传值的方法
ASP.NET页面传值的方法 From:Refresh-air 在面试的时候,经常会遇到这样的问题,其实我们会对其中的几种方法比较熟悉,因为项目中经常使用.但是要全面的回答ASP.NET中页面传值的方 ...
- WebForm 页面传值
一.使用Querystring Querystring是一种非常简单的传值方式,其缺点就是会把要传送的值显示在浏览器的地址栏中,并且在此方法中不能够传递对象.如果你想传递一个安全性不是那么太重要或者是 ...
- webform页面传值和删除修改
一.webform跨页面传值1.内置对象地址栏数据拼接 QueryString 优点:简单好用:速度快:不消耗服务器内存. 缺点:只能传字符串:保密性差(调转页面后在地址栏显示):长度有限.响应请求对 ...
- 微信小程序——详细讲解页面传值(多种方法)
1.使用navigator的url带参传值 (1)在pageA页面有一个固定的值要传递到pageB页面,比如说一个固定的值user_id要传递给B <navigator url=".. ...
- php页面传值的方法(转)
原文链接:https://www.cnblogs.com/suvllian/p/5582540.html PHP页面间传值的几种方法 方法一:require_once //Page a: < ...
- Asp.Net页面传值的方法简单总结【原创】
1.QueryString 当页面上form按照get的方式向页面发送请求数据的时候,web server会将请求数据放入 一个QEURY_STRING的环境变量中,然后通过QeueryString方 ...
- asp.net mvc 页面传值的方法总结
转自:http://msprogrammer.serviciipeweb.ro/2012/01/15/usual-methods-to-transfer-data-from-page-to-page- ...
随机推荐
- android webview如何加载asset目录里的页面
在asset里的页面都可以这样获得 file:///android_asset/index.html
- python 建立网站
python建立网站相关学习资源: 1. 一整套教程:http://www.pythondoc.com/flask-mega-tutorial/helloworld.html 2. 知乎关于这个问题的 ...
- 扩展KVM镜像的虚拟磁盘大小
当我们需要扩展模板镜像的虚拟磁盘大小时,比如原来的虚拟磁盘大小为20G,现在我们想将其扩展到30G,那么我们可以根据如下步骤来操作. 整个流程可以分为三个阶段: 1.扩展KVM镜像磁盘文件大小到30G ...
- CoreOS Linux available in China
CoreOS Linux 竭诚服务中国用户 今天,我们宣布一个令人振奋的消息 - CoreOS Linux 开源版本正式向中国地区提供服务!国内的用户们现在可以使用安全.自动的 CoreOS Linu ...
- ZOJ 1049 I Think I Need a Houseboat
原题链接 题目大意:Fred想在Louisiana买一套房子,但是堤坝不牢固,每年都要被河水侵蚀50平方英里.题目给出他豪宅的坐标,要求他被迫移民搬迁的年份. 解法:也没什么好说的,先求出两点间的距离 ...
- Docker仓库管理
1.# docker pull registry //下载registry镜像,registry为docker官方提供的一个镜像,我们可以用它来创建本地的docker私有仓库. 2.# docker ...
- POJ1459 Power Network(网络最大流)
Power Network Time Limit: 2000MS Memory Limit: 32768K Total S ...
- thinkPHP 中去除URL中的index.php
例如你的原路径是 http://localhost/app/index.php/module/action/var/value/ 那么现在的地址是 http://localhost/app/modul ...
- Python中变量的作用域(variable scope)
http://www.crifan.com/summary_python_variable_effective_scope/ 解释python中变量的作用域 示例: 1.代码版 #!/usr/bin/ ...
- 常见半监督方法 (SSL) 代码总结
经典以及最新的半监督方法 (SSL) 代码总结 最近因为做实验需要,收集了一些半监督方法的代码,列出了一个清单: 1. NIPS 2015 Semi-Supervised Learning with ...