webform 页面传值的方法总结
ASP.NET頁面之間傳遞值的幾種方式
頁面傳值是學習asp.net初期都會面臨的一個問題,總的來說有頁面傳值、存儲對象傳值、ajax、類、model、表單等。但是一般來說,常用的較簡單有QueryString,Session,Cookies,Application,Server.Transfer。
?
一、QueryString
QueryString是一種非常簡單的傳值方式,他可以將傳送的值顯示在瀏覽器的地址欄中。如果是傳遞一個或多個安全性要求不高或是結構簡單的數值時,可以使用這個方法。但是對于傳遞數組或對象的話,就不能用這個方法了。
這種方法的優點:1.使用簡單,對于安全性要求不高時傳遞數字或是文本值非常有效。
這種方法的缺點:1.缺乏安全性,由于它的值暴露在瀏覽器的URL地址中的。
2.不能傳遞對象。
使用方法:1.在源頁面的代碼中用需要傳遞的名稱和值構造URL地址。
2.在源頁面的代碼用Response.Redirect(URL);重定向到上面的URL地址中。
3.在目的頁面的代碼使用Request.QueryString["name"];取出URL地址中傳遞的值。
例子:(1)a.aspx
private void Button1_Click(object sender, System.EventArgs e) { string s_url; s_url = "b.aspx?name=" + Label1.Text; Response.Redirect(s_url); }(2)b.aspx
private void Page_Load(object sender, EventArgs e) { Label2.Text = Request.QueryString["name"]; }?
二、Session
想必這個肯定是大家使用中最常見的用法了,其操作與Application類似,作用于用戶個人,所以,過量的存儲會導致服務器內存資源的耗盡。
優點:1.使用簡單,不僅能傳遞簡單數據類型,還能傳遞對象。
2.數據量大小是不限制的。
缺點:1.在Session變量存儲大量的數據會消耗較多的服務器資源。
2.容易丟失。
使用方法:1.在源頁面的代碼中創建你需要傳遞的名稱和值構造Session變量:Session["Name"]="Value(Or Object)";
2.在目的頁面的代碼使用Session變量取出傳遞的值。Result = Session["Nmae"]
注意:session不用時可以銷毀它,銷毀的方法是:清除一個:Session.Remove("session名");
清除所有:Session.Clear();
例子:(1)a.aspx
private void Button1_Click(object sender, System.EventArgs e) { Session["name"] = Label.Text; }(2)b.aspx
private void Page_Load(object sender, EventArgs e) { string name; name = Session["name"].ToString(); }?
三、Cookie
這個也是大家常使用的方法,Cookie用于在用戶瀏覽器上存儲小塊的信息,保存用戶的相關信息,比如用戶訪問某網站時用戶的ID,用戶的偏好等,用戶下次訪問就可以通過檢索獲得以前的信息。所以Cookie也可以在頁面間傳遞值。Cookie通過HTTP頭在瀏覽器和服務器之間來回傳遞的。Cookie只能包含字符串的值,如果想在Cookie存儲整數值,那么需要先轉換為字符串的形式。
與Session一樣,其是什對每一個用戶而言的,但是有個本質的區別,即Cookie是存放在客戶端的,而session是存放在服務器端的。而且Cookie的使用要配合ASP.NET內置對象Request來使用。
優點:1.使用簡單,是保持用戶狀態的一種非常常用的方法。比如在購物網站中用戶跨多個頁面表單時可以用它來保持用戶狀態。
缺點:1.常常被人認為用來收集用戶隱私而遭到批評。
2.安全性不高,容易偽造。
使用方法:1.在源頁面的代碼中創建你需要傳遞的名稱和值構造Cookie對象:
HttpCookie objCookie = new HttpCookie("myCookie","Hello,Cookie!"); Response.Cookies.Add(cookie);2.在目的頁面的代碼使用Cookie對象取出傳遞的值:Result = Request.Cookies[ "myCookie" ].Value;
例子:(1)a.aspx
private void Button1_Click(object sender, System.EventArgs e) {HttpCookie objCookie = new HttpCookie("myCookie","Hello,Cookie!");Response.Cookies.Add(objCookie); }(2)b.aspx
string myName1Value; myName1Value = Request.Cookies[ "myCookie" ].Value;?
四、Application
Application對象的作用范圍是整個全局,也就是說對所有用戶都有效。它在整個應用程序生命周期中都是有效的,類似于使用全局變量一樣,所以可以在不同頁面中對它進行存取。它和Session變量的區別在于,前者是所有的用戶共用的全局變量,后者是各個用戶獨有的全局變量。
可能有人會問,既然所有用戶都可以使用application變量,那他可以用在什么場合呢?這里舉個例子:網站訪問數。多個請求訪問時都可以對它進行操作。
優點:1.使用簡單,消耗較少的服務器資源。
2.不僅能傳遞簡單數據,還能傳遞對象。
3.數據量大小是不限制的。
缺點:1.作為全局變量容易被誤操作。所以單個用戶使用的變量一般不能用application。
使用方法:1.在源頁面的代碼中創建你需要傳遞的名稱和值構造Application變量:Application["Nmae"]="Value(Or Object)";
2.在目的頁面的代碼使用Application變量取出傳遞的值。Result = Application["Nmae"]
注意:常用lock和unlock方法用來鎖定和解鎖,為了防止并發修改。
例子:(1)a.aspx
private void Button1_Click(object sender, System.EventArgs e) { Application["name"] = Label1.Text; }(2)b.aspx
private void Page_Load(object sender, EventArgs e) { string name; Application.Lock(); name = Application["name"].ToString(); Application.UnLock(); }?
五、Server.Transfer
這個才可以說是面象對象開發所使用的方法,其使用Server.Transfer方法把流程從當前頁面引導到另一個頁面中,新的頁面使用前一個頁面的應答流,所以這個方法是完全面象對象的,簡潔有效。
Server.Transfer是從當前的ASPX頁面轉到新的ASPX頁面,服務器端執行新頁并輸出,在新頁面中通過Context.Handler來獲得前一個頁面傳遞的各種數據類型的值、表單數據、QueryString.由于重定向完全在服務器端完成,所以客戶端瀏覽器中的URL地址是不會改變的。調用Server.Transfer時,當前的ASPX頁面終止執行,執行流程轉入另一個ASPX頁面,但新的ASPX頁面仍使用前一ASPX頁面創建的應答流。
ps:比較Server.Transfer和Response.Redirect的區別。
(1)Server.Transfer在服務器端完成,所以客戶端瀏覽器中的URL地址是不會改變的;Response.Redirect是客戶端完成,向服務器端提出新的頁面處理請求,所以客戶端瀏覽器中的URL地址是會改變的。
(2)Server.Transfer在服務器端完成,不需要客戶端提出請求,減少了客戶端對服務器端提出請求。[2]
(3)Server.Transfer只能夠轉跳到本地虛擬目錄指定的頁面,也就是工程項目中的頁面,而Response.Redirect則十分靈活,可以跳轉到任何URL地址。
(4)Server.Transfer可以將前一個頁面的各種類型的值傳到新的頁面;Response.Redirect則只能借助URL中帶參數或是結合上面四種辦法把各種類型的值傳到新的頁面。
優點:1.直接在服務器端重定向,使用簡單方便,減少了客戶端對服務器端提出請求。
2.可以傳遞各種數據類型的值和控件的值。
缺點:1.客戶端瀏覽器中的URL地址是不改變,會導致在新的頁面可能出現一些意想不到的問題。比如如果源頁面和目的頁面不在同一個虛擬目錄或其子目錄下,那么使用相對路徑的圖片、超鏈接都會導致錯誤的指向。
使用方法:1.在源頁面的代碼中,使用Page類的Server.Transfer跳到另一個頁面傳遞頁面數據:Server.Transfer("b.aspx","false")。
2.在目的頁面中,使用Context.Handler來接收數據:FormerPage formerPage = (FormerPage)Context.Handler;?然后用formerPage的屬性和方法來獲取前一個頁面的值,或者直接用Context.Items["myParameter "]
例子:(1)a.aspx
public string Name { get{ return Label1.Text;} } private void Button1_Click(object sender, System.EventArgs e) { Server.Transfer("b.aspx"); }(2)b.aspx
private void Page_Load(object sender, EventArgs e) { a newWeb; //實例a窗體 newWeb = (source)Context.Handler; string name; name = newWeb.Name; }?
以上就是常用的幾種頁面間傳值的方法,我一般使用session和querystring來傳值,少數情況會使用到cookie。本篇文章僅僅是介紹這幾種方法的使用方法,內部原理沒有過多的解釋,關于session的存儲方式請參見:session的存儲方式和配置
參考文章:1、ASP.NET頁面之間傳遞值的幾種方式
2、ASP.NET頁面之間傳遞值的幾種方法
接下來再補充幾個。。。。
?
Each button in the demo has the required code in it's Click event handler, and brings you to a separate target page that gets and displays the data that was sent. Here are the methods:
1. Use the querystring:
protected?void?QueryStringButton_Click(object sender, EventArgs e)
???? ????{
???????? ????Response.Redirect("QueryStringPage.aspx?Data="?+ Server.UrlEncode(DataToSendTextBox.Text));
????????}
2. Use HTTP POST:
<asp:Button ID="HttpPostButton" runat="server" Text="Use HttpPost"
???????? ????PostBackUrl="~/HttpPostPage.aspx" οnclick="HttpPostButton_Click"/>
protected?void?HttpPostButton_Click(object sender, EventArgs e)
????????{
?????// The PostBackUrl property of the Button takes care of where to send it!
????????}
??3. Use Session State:
???protected?void?SessionStateButton_Click(object sender, EventArgs e)
???? ????{
???????? ????Session["Data"] = DataToSendTextBox.Text;
???????? ????Response.Redirect("SessionStatePage.aspx");
????????}
??4.??Use public properties:
???public?string DataToSend
????????{
????????????get
????????????{
????????????? ???return?DataToSendTextBox.Text;
????????? ???}
????????}
????????protected?void?PublicPropertiesButton_Click(object sender, EventArgs e)
??? ?????{
??????? ?????Server.Transfer("PublicPropertiesPage.aspx");
????????}
5. Use PreviousPage Control Info:
protected?void?ControlInfoButton_Click(object sender, EventArgs e)
???? ????{
???????? ????Server.Transfer("ControlInfoPage.aspx");
????????}
??// target page:
protected?void?Page_Load(object sender, EventArgs e)
????????{
????????????var textbox = PreviousPage.FindControl("DataToSendTextbox")?as?TextBox;
??????????? ?if?(textbox !=?null)
????????????{
????????????????DataReceivedLabel.Text = textbox.Text;
????????????}
????????}
6. Use HttpContext Items Collection:
??protected?void?HttpContextButton_Click(object sender, EventArgs e)
???? ????{
???????? ????HttpContext.Current.Items["data"] = DataToSendTextBox.Text;
???????? ????Server.Transfer("HttpContextItemsPage.aspx");
????????}
// target page:
protected?void?Page_Load(object sender, EventArgs e)
?????? ??{
?????????? ??this.DataReceivedLabel.Text =(String) HttpContext.Current.Items["data"];
????????}
7. Use Cookies:
protected?void?CookiesButton_Click(object sender, EventArgs e)
????????{
????????????HttpCookie cook =??new?HttpCookie("data");
????????????cook.Expires = DateTime.Now.AddDays(1);
????????????cook.Value = DataToSendTextBox.Text;
???????? ????Response.Cookies.Add(cook);
??????????? ?Response.Redirect("HttpCookiePage.aspx");
????????}
// target page:
protected?void?Page_Load(object sender, EventArgs e)
????????{
????????????DataReceivedLabel.Text = Request.Cookies["data"].Value;
????????}
8. Use Cache:
protected?void?CacheButton_Click(object sender, EventArgs e)
????? ???{
????????? ???Cache["data"] = DataToSendTextBox.Text;
???????? ????Server.Transfer("CachePage.aspx");
????????}
???// target page:
????protected?void?Page_Load(object sender, EventArgs e)
?????? ??{
?????????? ??this.DataReceivedLabel.Text = (string) Cache["data"];
????????}
Actually, there are a number of other methods. You can use a database, a Data Store such as Redis or?BPlusTree, file storage, or even the Appdomain Cache. But as these are not as common, we'll leave those as an exercise for the reader. I should mention that the use of Cache and Application (not shown) are not user - specific, but they can easily be made so by prepending the key used with something unique to the user such as their SessionId.?
You can download the complete?Visual Studio 2010 demo solution here.
總結
以上是生活随笔為你收集整理的webform 页面传值的方法总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS中的!important属性用法
- 下一篇: “”和“” java