日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

webform 页面传值的方法总结

發(fā)布時(shí)間:2023/12/4 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 webform 页面传值的方法总结 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ASP.NET頁面之間傳遞值的幾種方式

  頁面?zhèn)髦凳菍W(xué)習(xí)asp.net初期都會(huì)面臨的一個(gè)問題,總的來說有頁面?zhèn)髦怠⒋鎯?chǔ)對(duì)象傳值、ajax、類、model、表單等。但是一般來說,常用的較簡(jiǎn)單有QueryString,Session,Cookies,Application,Server.Transfer。

?

  一、QueryString

  QueryString是一種非常簡(jiǎn)單的傳值方式,他可以將傳送的值顯示在瀏覽器的地址欄中。如果是傳遞一個(gè)或多個(gè)安全性要求不高或是結(jié)構(gòu)簡(jiǎn)單的數(shù)值時(shí),可以使用這個(gè)方法。但是對(duì)于傳遞數(shù)組或?qū)ο蟮脑?#xff0c;就不能用這個(gè)方法了。

  這種方法的優(yōu)點(diǎn):1.使用簡(jiǎn)單,對(duì)于安全性要求不高時(shí)傳遞數(shù)字或是文本值非常有效。
  這種方法的缺點(diǎn):1.缺乏安全性,由于它的值暴露在瀏覽器的URL地址中的。
          2.不能傳遞對(duì)象。

  使用方法:1.在源頁面的代碼中用需要傳遞的名稱和值構(gòu)造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

  想必這個(gè)肯定是大家使用中最常見的用法了,其操作與Application類似,作用于用戶個(gè)人,所以,過量的存儲(chǔ)會(huì)導(dǎo)致服務(wù)器內(nèi)存資源的耗盡。

  優(yōu)點(diǎn):1.使用簡(jiǎn)單,不僅能傳遞簡(jiǎn)單數(shù)據(jù)類型,還能傳遞對(duì)象。
     2.數(shù)據(jù)量大小是不限制的。

  缺點(diǎn):1.在Session變量存儲(chǔ)大量的數(shù)據(jù)會(huì)消耗較多的服務(wù)器資源。

     2.容易丟失。

  使用方法:1.在源頁面的代碼中創(chuàng)建你需要傳遞的名稱和值構(gòu)造Session變量:Session["Name"]="Value(Or Object)";

       2.在目的頁面的代碼使用Session變量取出傳遞的值。Result = Session["Nmae"]

  注意:session不用時(shí)可以銷毀它,銷毀的方法是:清除一個(gè):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

  這個(gè)也是大家常使用的方法,Cookie用于在用戶瀏覽器上存儲(chǔ)小塊的信息,保存用戶的相關(guān)信息,比如用戶訪問某網(wǎng)站時(shí)用戶的ID,用戶的偏好等,用戶下次訪問就可以通過檢索獲得以前的信息。所以Cookie也可以在頁面間傳遞值。Cookie通過HTTP頭在瀏覽器和服務(wù)器之間來回傳遞的。Cookie只能包含字符串的值,如果想在Cookie存儲(chǔ)整數(shù)值,那么需要先轉(zhuǎn)換為字符串的形式。

  與Session一樣,其是什對(duì)每一個(gè)用戶而言的,但是有個(gè)本質(zhì)的區(qū)別,即Cookie是存放在客戶端的,而session是存放在服務(wù)器端的。而且Cookie的使用要配合ASP.NET內(nèi)置對(duì)象Request來使用。

  優(yōu)點(diǎn):1.使用簡(jiǎn)單,是保持用戶狀態(tài)的一種非常常用的方法。比如在購(gòu)物網(wǎng)站中用戶跨多個(gè)頁面表單時(shí)可以用它來保持用戶狀態(tài)。

  缺點(diǎn):1.常常被人認(rèn)為用來收集用戶隱私而遭到批評(píng)。

     2.安全性不高,容易偽造。

  

  使用方法:1.在源頁面的代碼中創(chuàng)建你需要傳遞的名稱和值構(gòu)造Cookie對(duì)象:

HttpCookie objCookie = new HttpCookie("myCookie","Hello,Cookie!"); Response.Cookies.Add(cookie);

      2.在目的頁面的代碼使用Cookie對(duì)象取出傳遞的值: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對(duì)象的作用范圍是整個(gè)全局,也就是說對(duì)所有用戶都有效。它在整個(gè)應(yīng)用程序生命周期中都是有效的,類似于使用全局變量一樣,所以可以在不同頁面中對(duì)它進(jìn)行存取。它和Session變量的區(qū)別在于,前者是所有的用戶共用的全局變量,后者是各個(gè)用戶獨(dú)有的全局變量。

  可能有人會(huì)問,既然所有用戶都可以使用application變量,那他可以用在什么場(chǎng)合呢?這里舉個(gè)例子:網(wǎng)站訪問數(shù)。多個(gè)請(qǐng)求訪問時(shí)都可以對(duì)它進(jìn)行操作。

  優(yōu)點(diǎn):1.使用簡(jiǎn)單,消耗較少的服務(wù)器資源。

     2.不僅能傳遞簡(jiǎn)單數(shù)據(jù),還能傳遞對(duì)象。

     3.數(shù)據(jù)量大小是不限制的。

  缺點(diǎn):1.作為全局變量容易被誤操作。所以單個(gè)用戶使用的變量一般不能用application。

  使用方法:1.在源頁面的代碼中創(chuàng)建你需要傳遞的名稱和值構(gòu)造Application變量:Application["Nmae"]="Value(Or Object)";

       2.在目的頁面的代碼使用Application變量取出傳遞的值。Result = Application["Nmae"]

  注意:常用lock和unlock方法用來鎖定和解鎖,為了防止并發(fā)修改。

  例子:(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

  這個(gè)才可以說是面象對(duì)象開發(fā)所使用的方法,其使用Server.Transfer方法把流程從當(dāng)前頁面引導(dǎo)到另一個(gè)頁面中,新的頁面使用前一個(gè)頁面的應(yīng)答流,所以這個(gè)方法是完全面象對(duì)象的,簡(jiǎn)潔有效。

  Server.Transfer是從當(dāng)前的ASPX頁面轉(zhuǎn)到新的ASPX頁面,服務(wù)器端執(zhí)行新頁并輸出,在新頁面中通過Context.Handler來獲得前一個(gè)頁面?zhèn)鬟f的各種數(shù)據(jù)類型的值、表單數(shù)據(jù)、QueryString.由于重定向完全在服務(wù)器端完成,所以客戶端瀏覽器中的URL地址是不會(huì)改變的。調(diào)用Server.Transfer時(shí),當(dāng)前的ASPX頁面終止執(zhí)行,執(zhí)行流程轉(zhuǎn)入另一個(gè)ASPX頁面,但新的ASPX頁面仍使用前一ASPX頁面創(chuàng)建的應(yīng)答流。

  ps:比較Server.Transfer和Response.Redirect的區(qū)別。
    (1)Server.Transfer在服務(wù)器端完成,所以客戶端瀏覽器中的URL地址是不會(huì)改變的;Response.Redirect是客戶端完成,向服務(wù)器端提出新的頁面處理請(qǐng)求,所以客戶端瀏覽器中的URL地址是會(huì)改變的。
    (2)Server.Transfer在服務(wù)器端完成,不需要客戶端提出請(qǐng)求,減少了客戶端對(duì)服務(wù)器端提出請(qǐng)求。[2]
    (3)Server.Transfer只能夠轉(zhuǎn)跳到本地虛擬目錄指定的頁面,也就是工程項(xiàng)目中的頁面,而Response.Redirect則十分靈活,可以跳轉(zhuǎn)到任何URL地址。
    (4)Server.Transfer可以將前一個(gè)頁面的各種類型的值傳到新的頁面;Response.Redirect則只能借助URL中帶參數(shù)或是結(jié)合上面四種辦法把各種類型的值傳到新的頁面。

  優(yōu)點(diǎn):1.直接在服務(wù)器端重定向,使用簡(jiǎn)單方便,減少了客戶端對(duì)服務(wù)器端提出請(qǐng)求。

     2.可以傳遞各種數(shù)據(jù)類型的值和控件的值。

  缺點(diǎn):1.客戶端瀏覽器中的URL地址是不改變,會(huì)導(dǎo)致在新的頁面可能出現(xiàn)一些意想不到的問題。比如如果源頁面和目的頁面不在同一個(gè)虛擬目錄或其子目錄下,那么使用相對(duì)路徑的圖片、超鏈接都會(huì)導(dǎo)致錯(cuò)誤的指向。

  使用方法:1.在源頁面的代碼中,使用Page類的Server.Transfer跳到另一個(gè)頁面?zhèn)鬟f頁面數(shù)據(jù):Server.Transfer("b.aspx","false")。

       2.在目的頁面中,使用Context.Handler來接收數(shù)據(jù):FormerPage formerPage = (FormerPage)Context.Handler;?然后用formerPage的屬性和方法來獲取前一個(gè)頁面的值,或者直接用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; //實(shí)例a窗體 newWeb = (source)Context.Handler; string name; name = newWeb.Name; }

?

  以上就是常用的幾種頁面間傳值的方法,我一般使用session和querystring來傳值,少數(shù)情況會(huì)使用到cookie。本篇文章僅僅是介紹這幾種方法的使用方法,內(nèi)部原理沒有過多的解釋,關(guān)于session的存儲(chǔ)方式請(qǐng)參見:session的存儲(chǔ)方式和配置

  

  參考文章:1、ASP.NET頁面之間傳遞值的幾種方式

       2、ASP.NET頁面之間傳遞值的幾種方法





接下來再補(bǔ)充幾個(gè)。。。。

?

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.

總結(jié)

以上是生活随笔為你收集整理的webform 页面传值的方法总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。