WebForm跨页面传值---内置对象

一、Response.Redirect和Server.Transfer

1、Response - 响应请求对象

string path = "Default2.aspx“;

(1)Response.Redirect(path); -- 重定向。path为所要跳转的界面的路径

(2)Response.Write("  aaaa  "); -输出内容。可以当检查作用,如Response.Write(" <script>alert('aaaa');</script> ");

QueryString 地址栏传值方式

  格式为:”路径?变量=“+所需要传的值。

优点:简单好用,速度快,不消耗服务器内存.
缺点:只能传字符串 ;保密性不好 ;长度有限.

对path进行修改

path = "Default2.aspx?a="+TextBox1.Text;--传一个值

path = "Default2.aspx?a="+TextBox1.Text+"&b="+TextBox2.Text;--传两个或多个值

response.redirect(path);----path为string类型

2、还有一种跳转方法:Server.Transfer(path);

 (1)在页面中设置将要传的值的控件

(2)在后台设置一个属性(get方法),用来接收传的值。

(3)设置跳转页面功能和按钮

(4)在第二个页面中(在接收传值页面的aspx页面中,添加<%@ Reference Page="~/GCSetting.aspx" %>才能创建发送值的页面实例),我们就可以在接收值页面的aspx.cs使用Context.Handler属性来获得前一个页面实例对象的引用,通过它,就可以使用存取前一个页面的控件的值了。

代码如下:将firstweb的值传到secondweb页面中去

firstweb:

    aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="firstweb.aspx.cs" Inherits="firstweb" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="Button1" runat="server" Text="跳到页面2" />
            <br />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
    </form>
</body>
</html>
View Code

   aspx.cs

using System;

public partial class firstweb : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
    }
    public string name
    {
        get { return this.TextBox1.Text.Trim(); }

    }
    private void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("secondweb.aspx");
    }
}
View Code

secondweb:

    aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="secondweb.aspx.cs" Inherits="secondweb" %>
<%@ Reference page="~/firstweb.aspx" %><%--引用这个,才能在secondweb.aspx.cs中创建firstweb的实例--%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <br />
            <input type="button" id="aa" value="跳到页面1 " />
        </div>
    </form>
</body>
</html>
<script>
    document.getElementById("aa").onclick = function () {
        self.location = "firstweb.aspx";
    };
</script>
View Code

   aspx.cs

using System;

public partial class secondweb : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        firstweb f1;
        f1 = Context.Handler as firstweb;
        if (f1 != null)
        {
            Label1.Text = f1.name;
        }

    }
}
View Code

二、request  接收    

格式:request["变量名"]。此变量为response.redirect()传值的变量,如上面的a,b.

Request - 获取请求对象

在Default2里面的Label1的代码:

Label1.Text= Request["a"];--a为传值时设定的变量a 
Label1.Text+=Request["b"];--b为传值时设定的变量b

 三、session 相对全局变量

赋值:session["key"]=任意值;-----key为自己设置的变量,”任意值“为自己要传的值,如label1.text或者TextBox1.Text;

取值:Button1.Text=session["key"].tostring();----在这里Button1.Text可以换成其他的。

(1)session["key"]的返回值时object,在进行取值时要进行类型转换

(2)相对全局变量的意思是:

        打开一个页面,复制页面地址。如果页面不关闭,这个session的值随便用;如果把这个页面关了,重新粘贴地址打开,那么网页就会报错,因为页面与这个session的联系已经断了。如果重新打开另一个界面,那就是另外一个session。

      相当于session是一个模型,是用来盛东西的,这个模型有好多个,你打开一个网页拿了一个模型,这要不扔这个模型,里面的值随便用。如果你把网页关闭,相当于把这个模型扔了,你就不能用这个模型了,相当于不能用这个网页了,然后你再去拿一个模型,那么通过第一个模型的网页就不能再用了,只能通过第二个模型来进行操作。

   ”全局“的意思是只要你给session赋值了,你在哪个网页都能用这个session。

 (3)session是在服务器上运行的 ,不能滥用,会造成服务器内存溢出,导致服务器崩溃,解决办法为:传的内容的大小尽可能的小。但是不用session的话,会造成服务器资源浪费。

(4)session相当于程序的全局变量。

(5)session的存在周期为20分钟。就像登陆淘宝,20分钟后你不操作,会让你重新登录密码。

(6)session可以穿任意类型的内容。

四、Cookies

cookies的作用和session的作用一样,这里说一下区别

(1)cookies可以当全局变量,可以传值,但是它只能传字符串。

(2)cookies 的存在周期只能是20分钟。

(3)会话cookies是在自己的电脑上的硬盘来运行的和保存的。

(4)持久cookies的保存时间是由网站的设计方来设定的(如7天免登陆)。

//按钮1的点击事件
        Response.Cookies["a"].Value = TextBox1.Text;//把文本框的内容传给cookies["a"]。
        Response.Redirect("Default2.aspx");//店家按钮跳转到页面Default2界面
        Response.Cookies["a"].Expires = DateTime.Now.AddDays(7);//设置cookies["a"]的保存状态为7天,即7天免登陆
        Response.Cookies["a"].Expires=DateTime.Now.AddDays(-1);//设置cookies["a"]为过期状态。

无论是session或者cookies,当传值的内容为汉字时,会显示乱码,那么就需要进行在当前界面编码,在下一个界面解码,这样就可以了。

//Default1的按钮1 的点击事件
   string a=  HttpUtility.UrlEncode(TextBox1.Text,System.Text.Encoding.GetEncoding("utf-8"));//对TextBox1的内容编译成a。
        Response.Cookies["a"].Value = a;//把a的值赋给cookies["a"]。
        Response.Redirect("Default2.aspx");//跳转界面Default2
//Default2的界面加载Page_Load
        string a = HttpUtility.UrlDecode(Request.Cookies["a"].Value,    System.Text.Encoding.GetEncoding("utf-8"));//对cookies["a]"的内容进行解码,赋值给a;
        Label1.Text =a;

五、Application

(1)它是全局对象,只要被创建出来,所有人取得值是一样的。相当于建立一个全局变量,给这个变量规定一个值,只要服务器不关,所有人都能用这个变量。

(2)它的保存时间是永久的。

(3)存在于服务器中。

原文地址:https://www.cnblogs.com/wwz-wwz/p/5969283.html