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" %>View Code
<!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>
aspx.cs
using System;public partial class firstweb : System.Web.UI.PageView Code
{
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");
}
}
secondweb:
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="secondweb.aspx.cs" Inherits="secondweb" %>View Code
<%@ 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>
aspx.cs
using System;public partial class secondweb : System.Web.UI.PageView Code
{
protected void Page_Load(object sender, EventArgs e)
{
firstweb f1; f1 = Context.Handler as firstweb;
if (f1 != null)
{
Label1.Text = f1.name;
}
}
}
二、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)存在於服務器中。
WebForm跨頁面傳值---內置對象