五种跨页面传值

两个页面,test1.aspx,test2.aspx。

 

ContractedBlock.gif ExpandedBlockStart.gif test1.aspx
 1None.gif<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="test1.aspx.cs" Inherits="test1" %>
 2None.gif
 3None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4None.gif
 5None.gif<html xmlns="http://www.w3.org/1999/xhtml">
 6None.gif<head runat="server">
 7None.gif    <title>Untitled Page</title>
 8None.gif</head>
 9None.gif<body>
10None.gif    <form id="form1" runat="server">
11None.gif    <div>
12None.gif    
13None.gif        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
14None.gif        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
15None.gif        <br />
16None.gif        <asp:Button ID="btServerTransfer" BackColor="Red" runat="server" onclick="ServerTransfer_Click" Text="Server.Transfer" />
17None.gif        <asp:Button ID="btResponseRedirect" BackColor="Orange" runat="server" Text="ResponseRedirect " 
18None.gif            onclick="btResponseRedirect_Click" />
19None.gif        <asp:Button ID="btSession" BackColor="Yellow" runat="server" Text="Session" 
20None.gif            onclick="btSession_Click" />
21None.gif        <asp:Button ID="btCookies" BackColor="Green" runat="server" Text="Cookies" 
22None.gif            onclick="btCookies_Click" />
23None.gif        <asp:Button ID="btApplication" BackColor="Blue" runat="server" Text="Application " 
24None.gif            onclick="btApplication_Click" />
25None.gif    
26None.gif    </div>
27None.gif    </form>
28None.gif</body>
29None.gif</html>

 

 

ContractedBlock.gif ExpandedBlockStart.gif test1.aspx.cs
 1None.gifusing System;
 2None.gifusing System.Configuration;
 3None.gifusing System.Data;
 4None.gifusing System.Linq;
 5None.gifusing System.Web;
 6None.gifusing System.Web.Security;
 7None.gifusing System.Web.UI;
 8None.gifusing System.Web.UI.HtmlControls;
 9None.gifusing System.Web.UI.WebControls;
10None.gifusing System.Web.UI.WebControls.WebParts;
11None.gifusing System.Xml.Linq;
12None.gif
13None.gifpublic partial class test1 : System.Web.UI.Page 
14ExpandedBlockStart.gifContractedBlock.gifdot.gif{
15InBlock.gif    protected void Page_Load(object sender, EventArgs e)
16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
17InBlock.gif
18ExpandedSubBlockEnd.gif    }

19InBlock.gif    protected void ServerTransfer_Click(object sender, EventArgs e)
20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
21InBlock.gif        Server.Transfer("test2.aspx",true); 
22InBlock.gif
23ExpandedSubBlockEnd.gif    }

24InBlock.gif    public string Name
25ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
26InBlock.gif        get
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28InBlock.gif            return TextBox1.Text;
29ExpandedSubBlockEnd.gif        }

30ExpandedSubBlockEnd.gif    }

31InBlock.gif
32InBlock.gif    public string EMail
33ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
34InBlock.gif        get
35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
36InBlock.gif            return TextBox2.Text;
37ExpandedSubBlockEnd.gif        }

38ExpandedSubBlockEnd.gif    }

39InBlock.gif    //false表示页面跳转时不要停止该页面活动,通常和try一起用来捕捉异常
40InBlock.gif    protected void btResponseRedirect_Click(object sender, EventArgs e)
41ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
42InBlock.gif        Response.Redirect("test2.aspx?Name=" + TextBox1.Text + "&Email=" + TextBox2.Text,false);
43ExpandedSubBlockEnd.gif    }

44InBlock.gif    protected void btSession_Click(object sender, EventArgs e)
45ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
46InBlock.gif        Session["Name"= TextBox1.Text;
47InBlock.gif        Session["Email"= TextBox2.Text;
48InBlock.gif        Response.Redirect("test2.aspx");
49ExpandedSubBlockEnd.gif    }

50InBlock.gif    protected void btCookies_Click(object sender, EventArgs e)
51ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
52InBlock.gif        //HttpCookie Cookie = new HttpCookie("Cookie1");
53InBlock.gif        //Cookie.Value["Name"] = TextBox1.Text;
54InBlock.gif        //Cookie.Value["Email"] = TextBox2.Text;
55InBlock.gif        //Response.Cookies.Add(Cookie);
56InBlock.gif        //和下面作用一样
57InBlock.gif        //接受变为Request.Cookies["Cookie1"]["Email"]
58InBlock.gif        HttpCookie cName = new HttpCookie("Name");
59InBlock.gif        HttpCookie cEmail = new HttpCookie("Email");
60InBlock.gif        cName.Value = TextBox1.Text;
61InBlock.gif        Response.Cookies.Add(cName);
62InBlock.gif        cEmail.Value = TextBox2.Text;
63InBlock.gif        Response.Cookies.Add(cEmail);
64InBlock.gif        Response.Redirect("test2.aspx");
65ExpandedSubBlockEnd.gif    }

66InBlock.gif    protected void btApplication_Click(object sender, EventArgs e)
67ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
68InBlock.gif        Application["Name"= TextBox1.Text;
69InBlock.gif        Application["Email"= TextBox2.Text;
70InBlock.gif        Response.Redirect("test2.aspx"); 
71InBlock.gif
72ExpandedSubBlockEnd.gif    }

73ExpandedBlockEnd.gif}

74None.gif

 

 

ContractedBlock.gifExpandedBlockStart.giftest2.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>

 

ContractedBlock.gifExpandedBlockStart.giftest2.aspx.cs
 1None.gifusing System;
 2None.gifusing System.Collections;
 3None.gifusing System.Configuration;
 4None.gifusing System.Data;
 5None.gifusing System.Linq;
 6None.gifusing System.Web;
 7None.gifusing System.Web.Security;
 8None.gifusing System.Web.UI;
 9None.gifusing System.Web.UI.HtmlControls;
10None.gifusing System.Web.UI.WebControls;
11None.gifusing System.Web.UI.WebControls.WebParts;
12None.gifusing System.Xml.Linq;
13None.gifusing System.Drawing;
14None.gif
15None.gifpublic partial class test2 : System.Web.UI.Page
16ExpandedBlockStart.gifContractedBlock.gifdot.gif{
17InBlock.gif    protected void Page_Load(object sender, EventArgs e)
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif        //server.transfer多用于有大量参数的传值
20InBlock.gif        //要传递的变量可以通过属性或方法来获得
21InBlock.gif        //直接在服务器端重定向,使用简单方便,减少了客户端对服务器端提出请求
22InBlock.gif        //地址栏url不变
23InBlock.gif        if (Context.Handler is test1)
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            test1 test1 = (test1)Context.Handler;
26InBlock.gif            Label1.Text = test1.Name;
27InBlock.gif            Label2.Text = test1.EMail;
28InBlock.gif            Label1.BackColor = Color.Red;
29InBlock.gif            Label2.BackColor = Color.Red;
30ExpandedSubBlockEnd.gif        }

31InBlock.gif            //一般用于传递的值少而安全性要求不高的情况下
32InBlock.gif            //所有的变量都能在浏览器的URL中看到,不用于传机密参数
33InBlock.gif            //不能传递对象
34InBlock.gif        else if (Request.QueryString["Name"!= null)
35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
36InBlock.gif            Label1.Text = Request.QueryString["Name"];
37InBlock.gif            Label2.Text = Request.QueryString["Email"];
38InBlock.gif            Label1.BackColor = Color.Orange;
39InBlock.gif            Label2.BackColor = Color.Orange;
40ExpandedSubBlockEnd.gif        }

41InBlock.gif            //Session在用户向服务端发出首次请求时被创建,而在用户关闭浏览器或异常发生时终止
42InBlock.gif            //Session还可能过期
43InBlock.gif            //在Session变量存储过多的数据会消耗比较多的服务器资源
44InBlock.gif            //应该使用一些清理动作来去除一些不需要的session来降低资源的无谓消耗
45InBlock.gif        else if (Session["Name"!= null && Session["Email"!= null)
46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
47InBlock.gif            Label1.Text = Session["Name"].ToString();
48InBlock.gif            Label2.Text = Session["Email"].ToString();
49InBlock.gif            Label1.BackColor = Color.Yellow;
50InBlock.gif            Label2.BackColor = Color.Yellow;
51ExpandedSubBlockEnd.gif        }

52InBlock.gif            //一些浏览器是不支持Cookies的
53InBlock.gif            //一个cookie实例可以保存多个值
54InBlock.gif            //Cookie用于在用户浏览器上存储小块的信息,保存用户的相关信息,用户下次访问就可以通过检索获得以前的信息
55InBlock.gif            //浏览器对 Cookie 的大小有限制,只有不超过 4096 字节才能保证被接受
56InBlock.gif            //Cookie通过HTTP头在浏览器和服务器之间来回传递的
57InBlock.gif            //Cookie只能包含字符串的值
58InBlock.gif            //可以通过遍历Request对象的Cookie集合可以获得所有的浏览器所有的Cookie
59InBlock.gif        else if (Request.Cookies["Name"!= null && Request.Cookies["Email"!= null)
60ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
61InBlock.gif            Label1.Text = Request.Cookies["Name"].Value;
62InBlock.gif            Label2.Text = Request.Cookies["Email"].Value;
63InBlock.gif            Label1.BackColor = Color.Green;
64InBlock.gif            Label2.BackColor = Color.Green;
65ExpandedSubBlockEnd.gif        }

66InBlock.gif            //Application变量并赋值,可以在网站(项目)的所有页面中获得它
67InBlock.gif            //应用计数器等需要全局变量的地方
68InBlock.gif        else if (Application["Name"!= null && Application["Email"!= null)
69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
70InBlock.gif            Label1.Text = Application["Name"].ToString();
71InBlock.gif            Label2.Text = Application["Email"].ToString();
72InBlock.gif            Label1.BackColor = Color.Blue;
73InBlock.gif            Label2.BackColor = Color.Blue;
74ExpandedSubBlockEnd.gif        }

75ExpandedSubBlockEnd.gif    }

76ExpandedBlockEnd.gif}

77None.gif

转载于:https://www.cnblogs.com/liuweicfyj/archive/2008/11/18/1335680.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值