ASP.NET頁面間值傳遞的幾種方法
ASP.NET頁面間值傳遞的方法主要是三種,QueryString,Session和Server.Transfer。實(shí)現(xiàn)頁面間值的傳遞,也就可以跨越不同的頁面,實(shí)現(xiàn)復(fù)雜的功能。
ASP.NET頁面間值傳遞***種方法:
通過URL鏈接地址傳遞
- send.aspx:
- protected void Button1_Click(object sender, EventArgs e)
- {
- Request.Redirect("Default2.aspx?username=honge");
- }
- receive.aspx:
- string username = Request.QueryString["username"];
這樣可以得到參數(shù)值。
ASP.NET頁面間值傳遞第二種方法:
通過post方式。
- send.aspx
- <form id="form1" runat="server" action="receive.aspx" method=post>
- <div>
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
- <asp:TextBox ID="username" runat="server"></asp:TextBox>
- </div>
- </form>
- receive.aspx
- string username = Ruquest.Form["receive"];
ASP.NET頁面間值傳遞第三種方法:
通過session
- send.aspx:
- protected void Button1_Click(object sender, EventArgs e)
- {
- Session["username"] = "honge";
- Request.Redirect("Default2.aspx");
- }
- receive.aspx:
- string username = Session["username"];
這樣可以得到參數(shù)值。
第四種方法:
通過Application
- send.aspx:
- protected void Button1_Click(object sender, EventArgs e)
- {
- Application["username"] = "honge";
- Request.Redirect("Default2.aspx");
- }
- receive.aspx:
- string username = Application["username"];
這樣可以得到參數(shù)值。
第五種方法:
通過Server.Transfer
- send.aspx:
- public string Name
- {
- get {
- return "honge";
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- Server.Transfer("Default2.aspx");
- }
- receive.aspx:
- send d = Context.Handler as send ;
- if (d != null)
- {
- Response.Write(d.Name);這樣可以得到參數(shù)值。
- }
如果在asp.net 2.0中還可以這樣用:通過PreviousPage
- PreviousPage d = Context.Handler as PreviousPage ;
- if (d != null)
- {
- Response.Write(d.Name);這樣可以得到參數(shù)值。
- }
也可以這樣用:
- send.aspx:
- <asp:Button ID="btnSubmit" runat="server" PostBackUrl="~/reveive.aspx" Text="Submit" />
- receive.aspx:
- <%@ PreviousPageType VirtualPath="~/Default.aspx" %>
- string name = PreviousPage.Name;
這樣可以得到參數(shù)值。
如果你的頁面中用到了MasterPage的話 Server.Transfer 傳遞的 PreviousPage就無效了,不知道這是什么原因.所以在用到MasterPage的話,***用Session或是Context.Items["username"]來實(shí)現(xiàn).
使用QueryString
使用QuerySting在頁面間傳遞值已經(jīng)是一種很老的機(jī)制了,這種方法的主要優(yōu)點(diǎn)是實(shí)現(xiàn)起來非常簡單,然而它的缺點(diǎn)是傳遞的值是會顯示在瀏覽器的地址欄上的(不安全),同時又不能傳遞對象,但是在傳遞的值少而安全性要求不高的情況下,這個方法還是一個不錯的方案。使用這種方法的步驟如下:
1,使用控件創(chuàng)建web表單(form)
2,創(chuàng)建可以返回表單的按鈕和鏈接按鈕
3,在按鈕或鏈接按鈕的單擊事件里創(chuàng)建一個保存URL的字符變量
4,在保存的URL里添加QueryString參數(shù)
5,使用Response.Redirect重定向到上面保存的URL
下面的代碼片斷演示了如何實(shí)現(xiàn)這個方法:
源頁面代碼:
- private void Button1_Click
- (object sender, System.EventArgs e)
- {
- string url;
- url="anotherwebform.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;
- Response.Redirect(url);
- }
目標(biāo)頁面代碼:
- private void Page_Load
- (object sender, System.EventArgs e)
- {
- Label1.Text=Request.QueryString["name"];
- Label2.Text=Request.QueryString["email"];
- }
使用Session變量
使用Session變量是可以在頁面間傳遞值的的另一種方式,在本例中我們把控件中的值存在Session變量中,然后在另一個頁面中使用它,以不同頁面間實(shí)現(xiàn)值傳遞的目的。但是,需要注意的是在Session變量存儲過多的數(shù)據(jù)會消耗比較多的服務(wù)器資源,在使用session時應(yīng)該慎重,當(dāng)然了,我們也應(yīng)該使用一些清理動作來去除一些不需要的session來降低資源的無謂消耗。使用Session變量傳遞值的一般步驟如下:
1,在頁面里添加必要的控件
2,創(chuàng)建可以返回表單的按鈕和鏈接按鈕
3,在按鈕或鏈接按鈕的單擊事件里,把控件的值添加到session變量里
4,使用Response.Redirect方法重定向到另一個頁面
5,在另一個頁面提取session的值,在確定不需要使用該session時,要顯式清除它
下面的代碼片斷演示了如何實(shí)現(xiàn)這個方法:
源頁面代碼:
- private void Button1_Click
- (object sender, System.EventArgs e)
- {
- file://textbox1 and textbox2 are webform
- file://controls
- Session["name"]=TextBox1.Text;
- Session["email"]=TextBox2.Text;
- Server.Transfer("anotherwebform.aspx");
- }
目標(biāo)頁面代碼:
- private void Page_Load
- (object sender, System.EventArgs e)
- {
- Label1.Text=Session["name"].ToString();
- Label2.Text=Session["email"].ToString();
- Session.Remove("name");
- Session.Remove("email");
- }
使用Server.Transfer
這個方法相比上面介紹的方法稍微復(fù)雜一點(diǎn),但在頁面間值傳遞中卻是特別有用的,使用該方法你可以在另一個頁面以對象屬性的方式來存取顯露的值,當(dāng)然了,使用這種方法,你需要額外寫一些代碼以創(chuàng)建一些屬性以便可以在另一個頁面訪問它,但是,這個方式帶來的好處也是顯而易見的??傮w來說,使用這種方法是簡潔的同時又是面向?qū)ο蟮?。使用這種方法的整個過程如下:
1,在頁面里添加必要的控件
2,創(chuàng)建返回值的Get屬性過程
3,創(chuàng)建可以返回表單的按鈕和鏈接按鈕
4,在按鈕單擊事件處理程序中調(diào)用Server.Transfer方法轉(zhuǎn)移到指定的頁面
5,在第二個頁面中,我們就可以使用Context.Handler屬性來獲得前一個頁面實(shí)例對象的引用,通過它,就可以使用存取前一個頁面的控件的值了
以下代碼綜合實(shí)現(xiàn)上述步驟過程的代碼:
源頁面代碼:
把以下的代碼添加到頁面中
- public string Name
- {
- get
- {
- return TextBox1.Text;
- }
- }
- public string EMail
- {
- get
- {
- return TextBox2.Text;
- }
- }
然后調(diào)用Server.Transfer方法
- private void Button1_Click
- (object sender, System.EventArgs e)
- {
- Server.Transfer("anotherwebform.aspx");
- }
目標(biāo)頁面代碼:
- private void Page_Load
- (object sender, System.EventArgs e)
- {
- file://create instance of source web form
- WebForm1 wf1;
- file://get reference to current handler instance
- wf1=(WebForm1)Context.Handler;
- Label1.Text=wf1.Name;
- Label2.Text=wf1.EMail;
- }
總結(jié)
本文講述了使用不同的方法實(shí)現(xiàn)了ASP.NET頁面間值傳遞,這三種方法是:QueryString,Session和Server.Transfer,我們應(yīng)該反覆體會幾種方法的異同。我希望本文能給你有用的助益,直到在你的代碼中運(yùn)用自如!
【編輯推薦】