1. 使用QueryString變量 QueryString是一種非常簡單的傳值方式,他可以將傳送的值顯示在瀏覽器的地址欄中。如果是傳遞一個或多個安全性要求不高或是結構簡單的數值時,可以使用這個方法。但是對于傳遞數組或對象的話,就不能用這個方法了。下面是一個例子: a.aspx的C#代碼 :
1 private void Button1_Click(object sender, System.EventArgs e) 2 { 3 string s_url; 4 s_url = "b.aspx?name=" + Label1.Text; 5 Response.Redirect(s_url); 6 }
b.aspx中C#代碼:
1 private void Page_Load(object sender, EventArgs e) 2 { 3 Label2.Text = Request.QueryString["name"]; 4 }
2. 使用Application 對象變量 Application對象的作用范圍是整個全局,也就是說對所有用戶都有效。其常用的方法用Lock和UnLock。
a.aspx的C#代碼:
1 private void Button1_Click(object sender, System.EventArgs e) 2 { 3 Application["name"] = Label1.Text; 4 Server.Transfer("b.aspx"); 5 }
b.aspx中C#代碼:
1 private void Page_Load(object sender, EventArgs e) 2 { 3 string name; 4 Application.Lock(); 5 name = Application["name"].ToString(); 6 Application.UnLock(); 7 }
3. 使用Session變量 想必這個肯定是大家使用中最常見的用法了,其操作與Application類似,作用于用戶個人,所以,過量的存儲會導致服務器內存資源的耗盡。 a.aspx的C#代碼:
1 private void Button1_Click(object sender, System.EventArgs e) 2 { 3 Session["name"] = Label.Text; 4 }
b.aspx中C#代碼 :
1 private void Page_Load(object sender, EventArgs e) 2 { 3 string name; 4 name = Session["name"].ToString(); 5 }
4. 使用Cookie對象變量 這個也是大家常使用的方法,與Session一樣,其是什對每一個用戶而言的,但是有個本質的區別,即Cookie是存放在客戶端的,而session是存放在服務器端的。而且Cookie的使用要配合ASP.NET內置對象Request來使用。a.aspx的C#代碼 :
1 private void Button1_Click(object sender, System.EventArgs e) 2 { 3 HttpCookie cookie_name = new HttpCookie("name"); 4 cookie_name.Value = Label1.Text; 5 Reponse.AppendCookie(cookie_name); 6 Server.Transfer("b.aspx"); 7 }
b.aspx中C#代碼:
private void Page_Load(object sender, EventArgs e) { string name; name = Request.Cookie["name"].Value.ToString(); }
5. 使用Server.Transfer方法 這個才可以說是面象對象開發所使用的方法,其使用Server.Transfer方法把流程從當前頁面引導到另一個頁面中,新的頁面使用前一個頁面的應答流,所以這個方法是完全面象對象的,簡潔有效。 a.aspx的C#代碼:
1 public string Name 2 { 3 get{ return Label1.Text;} 4 } 5 private void Button1_Click(object sender, System.EventArgs e) 6 { 7 Server.Transfer("b.aspx"); 8 }
b.aspx中C#代碼:
1 private void Page_Load(object sender, EventArgs e) 2 { 3 a newWeb; //實例a窗體 4 newWeb = (source)Context.Handler; 5 string name; 6 name = newWeb.Name; 7 }
?