两个页面,test1.aspx,test2.aspx。
![](/Images/OutliningIndicators/ContractedBlock.gif)
test1.aspx
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test1.aspx.cs" Inherits="test1" %>
2![](/Images/OutliningIndicators/None.gif)
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4![](/Images/OutliningIndicators/None.gif)
5
<html xmlns="http://www.w3.org/1999/xhtml">
6
<head runat="server">
7
<title>Untitled Page</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
13
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
14
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
15
<br />
16
<asp:Button ID="btServerTransfer" BackColor="Red" runat="server" onclick="ServerTransfer_Click" Text="Server.Transfer" />
17
<asp:Button ID="btResponseRedirect" BackColor="Orange" runat="server" Text="ResponseRedirect "
18
onclick="btResponseRedirect_Click" />
19
<asp:Button ID="btSession" BackColor="Yellow" runat="server" Text="Session"
20
onclick="btSession_Click" />
21
<asp:Button ID="btCookies" BackColor="Green" runat="server" Text="Cookies"
22
onclick="btCookies_Click" />
23
<asp:Button ID="btApplication" BackColor="Blue" runat="server" Text="Application "
24
onclick="btApplication_Click" />
25
26
</div>
27
</form>
28
</body>
29
</html>
![](/Images/OutliningIndicators/ContractedBlock.gif)
test1.aspx.cs
1
using System;
2
using System.Configuration;
3
using System.Data;
4
using System.Linq;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.HtmlControls;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.WebControls.WebParts;
11
using System.Xml.Linq;
12![](/Images/OutliningIndicators/None.gif)
13
public partial class test1 : System.Web.UI.Page
14![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
15
protected void Page_Load(object sender, EventArgs e)
16![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17![](/Images/OutliningIndicators/InBlock.gif)
18
}
19
protected void ServerTransfer_Click(object sender, EventArgs e)
20![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
Server.Transfer("test2.aspx",true);
22![](/Images/OutliningIndicators/InBlock.gif)
23
}
24
public string Name
25![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
get
27![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
return TextBox1.Text;
29
}
30
}
31![](/Images/OutliningIndicators/InBlock.gif)
32
public string EMail
33![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
get
35![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
return TextBox2.Text;
37
}
38
}
39
//false表示页面跳转时不要停止该页面活动,通常和try一起用来捕捉异常
40
protected void btResponseRedirect_Click(object sender, EventArgs e)
41![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
Response.Redirect("test2.aspx?Name=" + TextBox1.Text + "&Email=" + TextBox2.Text,false);
43
}
44
protected void btSession_Click(object sender, EventArgs e)
45![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
Session["Name"] = TextBox1.Text;
47
Session["Email"] = TextBox2.Text;
48
Response.Redirect("test2.aspx");
49
}
50
protected void btCookies_Click(object sender, EventArgs e)
51![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
//HttpCookie Cookie = new HttpCookie("Cookie1");
53
//Cookie.Value["Name"] = TextBox1.Text;
54
//Cookie.Value["Email"] = TextBox2.Text;
55
//Response.Cookies.Add(Cookie);
56
//和下面作用一样
57
//接受变为Request.Cookies["Cookie1"]["Email"]
58
HttpCookie cName = new HttpCookie("Name");
59
HttpCookie cEmail = new HttpCookie("Email");
60
cName.Value = TextBox1.Text;
61
Response.Cookies.Add(cName);
62
cEmail.Value = TextBox2.Text;
63
Response.Cookies.Add(cEmail);
64
Response.Redirect("test2.aspx");
65
}
66
protected void btApplication_Click(object sender, EventArgs e)
67![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
68
Application["Name"] = TextBox1.Text;
69
Application["Email"] = TextBox2.Text;
70
Response.Redirect("test2.aspx");
71![](/Images/OutliningIndicators/InBlock.gif)
72
}
73
}
74![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
test2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
![](/Images/OutliningIndicators/ContractedBlock.gif)
test2.aspx.cs
1
using System;
2
using System.Collections;
3
using System.Configuration;
4
using System.Data;
5
using System.Linq;
6
using System.Web;
7
using System.Web.Security;
8
using System.Web.UI;
9
using System.Web.UI.HtmlControls;
10
using System.Web.UI.WebControls;
11
using System.Web.UI.WebControls.WebParts;
12
using System.Xml.Linq;
13
using System.Drawing;
14![](/Images/OutliningIndicators/None.gif)
15
public partial class test2 : System.Web.UI.Page
16![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
17
protected void Page_Load(object sender, EventArgs e)
18![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
//server.transfer多用于有大量参数的传值
20
//要传递的变量可以通过属性或方法来获得
21
//直接在服务器端重定向,使用简单方便,减少了客户端对服务器端提出请求
22
//地址栏url不变
23
if (Context.Handler is test1)
24![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
test1 test1 = (test1)Context.Handler;
26
Label1.Text = test1.Name;
27
Label2.Text = test1.EMail;
28
Label1.BackColor = Color.Red;
29
Label2.BackColor = Color.Red;
30
}
31
//一般用于传递的值少而安全性要求不高的情况下
32
//所有的变量都能在浏览器的URL中看到,不用于传机密参数
33
//不能传递对象
34
else if (Request.QueryString["Name"] != null)
35![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
Label1.Text = Request.QueryString["Name"];
37
Label2.Text = Request.QueryString["Email"];
38
Label1.BackColor = Color.Orange;
39
Label2.BackColor = Color.Orange;
40
}
41
//Session在用户向服务端发出首次请求时被创建,而在用户关闭浏览器或异常发生时终止
42
//Session还可能过期
43
//在Session变量存储过多的数据会消耗比较多的服务器资源
44
//应该使用一些清理动作来去除一些不需要的session来降低资源的无谓消耗
45
else if (Session["Name"] != null && Session["Email"] != null)
46![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
Label1.Text = Session["Name"].ToString();
48
Label2.Text = Session["Email"].ToString();
49
Label1.BackColor = Color.Yellow;
50
Label2.BackColor = Color.Yellow;
51
}
52
//一些浏览器是不支持Cookies的
53
//一个cookie实例可以保存多个值
54
//Cookie用于在用户浏览器上存储小块的信息,保存用户的相关信息,用户下次访问就可以通过检索获得以前的信息
55
//浏览器对 Cookie 的大小有限制,只有不超过 4096 字节才能保证被接受
56
//Cookie通过HTTP头在浏览器和服务器之间来回传递的
57
//Cookie只能包含字符串的值
58
//可以通过遍历Request对象的Cookie集合可以获得所有的浏览器所有的Cookie
59
else if (Request.Cookies["Name"] != null && Request.Cookies["Email"] != null)
60![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
Label1.Text = Request.Cookies["Name"].Value;
62
Label2.Text = Request.Cookies["Email"].Value;
63
Label1.BackColor = Color.Green;
64
Label2.BackColor = Color.Green;
65
}
66
//Application变量并赋值,可以在网站(项目)的所有页面中获得它
67
//应用计数器等需要全局变量的地方
68
else if (Application["Name"] != null && Application["Email"] != null)
69![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
Label1.Text = Application["Name"].ToString();
71
Label2.Text = Application["Email"].ToString();
72
Label1.BackColor = Color.Blue;
73
Label2.BackColor = Color.Blue;
74
}
75
}
76
}
77![](/Images/OutliningIndicators/None.gif)