.net頁面間傳值的幾種經常使用方法

1。使用QueryString
      使用QuerySting在頁面間傳遞值已是一種很老的機制了,這種方法的主要優勢是實現起來很是簡單,然而它的缺點是傳遞的值是會顯示在瀏覽器的地址欄上的(不安全),同時又不能傳遞對象,可是在傳遞的值少而安全性要求不高的狀況下,這個方法仍是一個不錯的方案。使用這種方法的步驟以下:
1,使用控件建立web表單(form)
2,建立能夠返回表單的按鈕和連接按鈕
3,在按鈕或連接按鈕的單擊事件裏建立一個保存URL的字符變量
4,在保存的URL裏添加QueryString參數
5,使用Response.Redirect重定向到上面保存的URL
下面的代碼片段演示瞭如何實現這個方法:
  源頁面WebForm1.aspx.cs中的部分代碼:web

  
  
  
  
  1. private void Button1_Click(object sender, System.EventArgs e)  
  2. {  
  3.      string url;  
  4.      url="WebForm2.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;  
  5.      Response.Redirect(url);  
  6. }  
  7.  目標頁面WebForm2.aspx.cs中的部分代碼:  
  8. private void Page_Load(object sender, System.EventArgs e)  
  9. {  
  10.      Label1.Text=Request.QueryString["name"];  
  11.      Label2.Text=Request.QueryString["email"];  


2。使用Session變量
      使用Session變量是能夠在頁面間傳遞值的的另外一種方式,在本例中咱們把控件中的值存在Session變量中,而後在另外一個頁面中使用它,以不一樣頁面間實現值傳遞的目的。可是,須要注意的是在Session變量存儲過多的數據會消耗比較多的服務器資源,在使用session時應該慎重,固然了,咱們也應該使用一些清理動做來去除一些不須要的session來下降資源的無謂消耗。使用Session變量傳遞值的通常步驟以下:
1,在頁面裏添加必要的控件
2,建立能夠返回表單的按鈕和連接按鈕
3,在按鈕或連接按鈕的單擊事件裏,把控件的值添加到session變量裏
4,使用Response.Redirect(或Server.Transfer)方法重定向到另外一個頁面
5,在另外一個頁面提取session的值,在肯定不須要使用該session時,要顯式清除它
下面的代碼片段演示瞭如何實現這個方法:
   源頁面WebForm1.aspx.cs中的部分代碼:瀏覽器

  
  
  
  
  1. private void Button1_Click(object sender, System.EventArgs e)  
  2. {  
  3.      //textbox1 and textbox2 are webform  
  4.      //controls  
  5.      Session["name"]=TextBox1.Text;  
  6.      Session["email"]=TextBox2.Text;  
  7.      Server.Transfer("WebForm2.aspx");  
  8. }  
  9.   目標頁面WebForm2.aspx.cs中的部分代碼:  
  10. private void Page_Load(object sender, System.EventArgs e)  
  11. {  
  12.      Label1.Text=Session["name"].ToString();  
  13.      Label2.Text=Session["email"].ToString();  
  14.      Session.Remove("name");  
  15.      Session.Remove("email");  
  16. }  

3.使用Server.Transfer
      這個方法相比上面介紹的方法稍微複雜一點,但在頁面間值傳遞中倒是特別有用的,使用該方法你能夠在另外一個頁面以對象屬性的方式來存取顯露的值,固然了,使用這種方法,你須要額外寫一些代碼以建立一些屬性以即可以在另外一個頁面訪問它,可是,這個方式帶來的好處也是顯而易見的。整體來講,使用這種方法是簡潔的同時又是面向對象的。使用這種方法的整個過程以下:
1,在頁面裏添加必要的控件
2,建立返回值的Get屬性過程
3,建立能夠返回表單的按鈕和連接按鈕
4,在按鈕單擊事件處理程序中調用Server.Transfer方法轉移到指定的頁面
5,在第二個頁面中,咱們就可使用Context.Handler屬性來得到前一個頁面實例對象的引用,經過它,就可使用存取前一個頁面的控件的值了
如下代碼綜合實現上述步驟過程的代碼:
  源頁面WebForm1.aspx.cs中的部分代碼:
    把如下的代碼添加到頁面中
 安全

  
  
  
  
  1. public string Name  
  2. {  
  3.      get  
  4.      {  
  5.          return TextBox1.Text;  
  6.      }  
  7. }  
  8.  
  9. public string EMail  
  10. {  
  11.      get  
  12.      {  
  13.          return TextBox2.Text;  
  14.      }  
  15. }  
  16.   而後調用Server.Transfer方法  
  17. private void Button1_Click(object sender, System.EventArgs e)  
  18. {  
  19.      Server.Transfer("WebForm2.aspx");  
  20. }  
  21.    目標頁面代碼:  
  22.  
  23. 在WebForm2.aspx中務必在第一句話添加  
  24.  
  25. <%@ Reference Page="~/WebForm1.aspx" %>或  
  26.  
  27. <%@ PreviousPageType VirtualPath="~/WebForm1.aspx" %> 
  28.  
  29. 而後在WebForm2.aspx.cs中添加以下。  
  30.  
  31.  
  32. private void Page_Load(object sender, System.EventArgs e)  
  33. {  
  34.      //create instance of source web form  
  35.      WebForm1 wf1;  
  36.      //get reference to current handler instance  
  37.      wf1=(WebForm1)Context.Handler;  
  38.      Label1.Text=wf1.Name;  
  39.      Label2.Text=wf1.EMail;  
  40. }  
  41.  

若是在調試的過程當中遇到錯誤.就到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files中把新建的網站名的文件夾刪掉.(要先關掉Visual Studio開發環境再刪)服務器

4.使用@PreviousPageType指令cookie

      這個指令是.net 2.0中的一個新指令,用於處理ASP.NET 2.0提供的跨頁面傳送新功能.用於批定跨頁面的傳送過程起始於哪一個頁面.包含兩個屬性:session

TypeName:設置回送時的派生類名ide

VirtualPath:設置回送時所傳送頁面的地址.網站

以下示例:this

  
  
  
  
  1. 源頁面WebForm1.aspx中有一個TextBox,ID爲txtName.在WebForm1.aspx.cs中設置一個屬性:  
  2.  
  3. public TextBox Name  
  4.  
  5. {  
  6.  
  7.     get{return this.txtName;}//返回一個控件對象  
  8.  
  9. }  
  10.  
  11. 在目標頁面的設計文件中(WebForm2.aspx)的最上方加上:  
  12.  
  13.  <%@ PreviousPageType VirtualPath="~/Page1.aspx"%>,  
  14.  
  15. 而後就能引用WebForm1.aspx中定義的屬性了.  
  16.  
  17. 在WebForm2.aspx.cs中能夠有以下引用形式(假設WebForm2.aspx中有一個ID爲lblName的Label):  
  18.  
  19. lblName.Text="Hello"+PreviousPage.Name.Text+"<br />";  
  20.  

 5.利用某些控件的PostBackUrl屬性url

示例:仍然是源頁面WebForm1.aspx和目標頁面WebForm2.aspx.

WebForm1.aspx中的部分代碼:

 

  
  
  
  
  1. <asp:Button ID="btnPostBack" Runat="server" Text="PBButton"></asp:Button> 
  2.  
  3. <asp:TextBox ID="txtName" Runat="server" ></asp:TextBox> 
  4.  
  5. <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar> 
  6.  
  7. WebForm2.aspx.cs中的部分代碼:  
  8.  
  9. protected void Page_Load(object Sender,System.EventArgs e)  
  10.  
  11. {  
  12.  
  13.     TextBox txtName;  
  14.  
  15.     Calendar calendar1;  
  16.  
  17.     txtName=(TextBox)PreviousPage.FindControl("txtName");  
  18.  
  19.     calendar1=(Calendar)PreviousPage.FindControl("Calendar1");  
  20.  
  21.     Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();  
  22.  
  23. }  
  24.  

使用這種方法存在一個問題:若是在沒有單擊那個按鈕以前,也就是未處理WebForm1.aspx以前,有人請求了WebForm2.aspx,該怎麼辦?這就須要在WebForm2.aspx中的代碼處理以前加一個判斷.使用IsCrossPagePostBack屬性,這與IsPostBack屬性很類似,它容許檢查請求是否來自WebForm1.aspx.以下:

  
  
  
  
  1. protected void Page_Load(object Sender,System.EventArgs e)  
  2.  
  3. {  
  4.  
  5.     if(PreviousPage.IsCrossPagePostBack)  
  6.  
  7.     {  
  8.  
  9.         TextBox txtName;  
  10.  
  11.         Calendar calendar1;  
  12.  
  13.         txtName=(TextBox)PreviousPage.FindControl("txtName");  
  14.  
  15.         calendar1=(Calendar)PreviousPage.FindControl("Calendar1");  
  16.  
  17.         Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();  
  18.  
  19.     }  
  20.  
  21.     else  
  22.  
  23.     {  
  24.  
  25.         Response.Redirect("WebForm1.aspx");  
  26.  
  27.     }  
  28.  
  29. }  
  30.  

6.  使用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. }  
  8.  
  9. b.aspx中C#代碼  
  10. private void Page_Load(object sender, EventArgs e)  
  11. {  
  12.     string name;  
  13.     name = Request.Cookie["name"].Value.ToString();  
  14. }  
  15.  

7.  使用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. }  
  6.  
  7. b.aspx中C#代碼  
  8. private void Page_Load(object sender, EventArgs e)  
  9. {  
  10.     string name;  
  11.     Application.Lock();  
  12.     name = Application["name"].ToString();  
  13.     Application.UnLock();  
  14. }  
  15.  

 

 轉載出處:http://blog.csdn.net/MicrosoftCenterOfHN/archive/2009/05/12/4170404.aspx

相關文章
相關標籤/搜索