溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

[ASP.NET]跨頁面傳值

發布時間:2020-05-10 13:30:45 來源:網絡 閱讀:396 作者:蓬萊仙羽 欄目:編程語言

1. 使用QueryString變量

QueryString是一種非常簡單的傳值方式,他可以將傳送的值顯示在瀏覽器的地址欄中。如果是傳遞一個或多個安全性要求不高或是結構簡單的數值時,可以使用這個方法。但是對于傳遞數組或對象的話,就不能用這個方法了。下面是一個例子:

[csharp] view plaincopyprint?[ASP.NET]跨頁面傳值[ASP.NET]跨頁面傳值
  1. //ask.aspx的代碼   
  2. protected void Button1_Click(object sender,EventArgs e)  
  3. {  
  4.       Response.Redirect("answer.aspx?name=天天");  
  5. }  
  6.   
  7. //answer.aspx中代碼   
  8. protectedvoid Page_Load(object sender, EventArgs e)  
  9. {  
  10.      Response.Write(Request.QueryString["name"]);  
  11. }  

如下圖所示,傳遞的參數包含在了URL,也因為URL的長度畢竟有限,所以這種方式傳遞的參數一般都是安全性要求不高結構簡單的數值.


                                                                    [ASP.NET]跨頁面傳值

2. 使用Application 對象變量

Application對象的作用范圍是整個全局,也就是說對所有用戶都有效。其常用Lock和UnLock方法來對application對象進行鎖定,防止多個用戶同時對次此對象進行操作,造成數據混亂。并且此對象類似與哈希表,采用鍵值對來存儲數據.如下:

[csharp] view plaincopyprint?[ASP.NET]跨頁面傳值[ASP.NET]跨頁面傳值
  1. //ask.aspx的代碼  
  2. private void Button1_Click(object sender, System.EventArgs e)  
  3. {  
  4.     Application["name"] = "天天";  
  5.     Server.Transfer("answer.aspx");  
  6. }  
  7. //answer.aspx中代碼  
  8. private void Page_Load(object sender, EventArgs e)  
  9. {  
  10. <span style="white-space:pre">  </span>Application.Lock();  
  11. <span style="white-space:pre">  </span>Response.Write(Application["name"].ToString());  
  12. <span style="white-space:pre">  </span>Application.UnLock();  
  13. }  

                                                                          [ASP.NET]跨頁面傳值

這里有一點需要注意的是使用Server.Transfer跳轉頁面的時候瀏覽器顯示的地址并沒有改變,如果不清楚的話,可能會引起誤會.

3. 使用Session變量

想必這個肯定是大家使用中最常見的用法了,其操作與Application類似,作用于用戶個人,所以,過量的存儲會導致服務器內存資源的耗盡。

ask.aspx的代碼

[csharp] view plaincopyprint?[ASP.NET]跨頁面傳值[ASP.NET]跨頁面傳值
  1. private void Button1_Click(object sender, System.EventArgs e)  
  2. {  
  3. <span style="white-space:pre">  </span>Session["name"] = "天天";  
  4. <span style="white-space:pre">  </span>Response.Redirect ("answer.aspx");  
  5. }  
  6. //answer.aspx中代碼  
  7. private void Page_Load(object sender, EventArgs e)  
  8. {  
  9. <span style="white-space:pre">  </span>Response.Write(Session["name"].ToString());  
  10. }  


                                                         [ASP.NET]跨頁面傳值

4. 使用Cookie對象變量

這個也是大家常使用的方法,與Session一樣,其是什對每一個用戶而言的,但是有個本質的區別,即Cookie是存放在客戶端的,而session是存放在服務器端的。而且Cookie的使用要配合ASP.NET內置對象Request來使用。

[csharp] view plaincopyprint?[ASP.NET]跨頁面傳值[ASP.NET]跨頁面傳值
  1. //ask.aspx的代碼  
  2. private void Button1_Click(object sender, System.EventArgs e)  
  3. {  
  4.       HttpCookiecookieTest = new HttpCookie("name"); //創建cookie對象  
  5.       cookieTest.Value ="天天";                      //給cookie對象賦值  
  6.       Response.AppendCookie(cookieTest);              //保存cookies對象(response.Cookie.Add(CookieName);)  
  7.       erver.Transfer("answer.aspx");  
  8. }  
  9. //answer.aspx中代碼  
  10. private void Page_Load(object sender, EventArgs e)  
  11. {  
  12.      HttpCookie MyCookie= Request.Cookies["name"];//獲取Cookie對象  
  13.      String StrName =MyCookie.Value;   //獲取其變量值  
  14.      Response.Write(StrName);  
  15. }  

[ASP.NET]跨頁面傳值

5.使用cache緩存的方式

當然緩存存在的時間不是太長所以需要慎重使用

[csharp] view plaincopyprint?[ASP.NET]跨頁面傳值[ASP.NET]跨頁面傳值
  1. //ask.aspx的代碼  
  2.  protected void Button1_Click(object sender,EventArgs e)  
  3.  {  
  4.            this.Cache.Insert("name""天天");  
  5.            Server.Transfer("answer.aspx");  
  6. }  
  7.   
  8. //answer.aspx中代碼  
  9. protectedvoid Page_Load(object sender, EventArgs e)  
  10. {  
  11.            Response.Write(Cache["name"].ToString());  
  12. }  

[ASP.NET]跨頁面傳值

6. 使用Server.Transfer方法

這個才可以說是面象對象開發所使用的方法,其使用Server.Transfer方法把流程從當前頁面引導到另一個頁面中,新的頁面使用前一個頁面的應答流,所以這個方法是完全面象對象的,簡潔有效。

[csharp] view plaincopyprint?[ASP.NET]跨頁面傳值[ASP.NET]跨頁面傳值
  1. //ask.aspx的代碼  
  2. public string name  
  3. {  
  4.      get  
  5.      {  
  6.          return "天天";  
  7.       }  
  8. }  
  9. private void Button1_Click(object sender, System.EventArgs e)  
  10. {  
  11. <span style="white-space:pre">  </span>Server.Transfer("answer.aspx");  
  12. }  
  13.   
  14. //answer.aspx中C#代碼  
  15. private void Page_Load(object sender, EventArgs e)  
  16. {  
  17.      ask a = new ask();  
  18.      string StrName  = a.name;  
  19.      Response.Write(StrName);  
  20. }  

[ASP.NET]跨頁面傳值

錯誤紕漏再所難免,歡迎蒞臨指正

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女