asp.net頁面跳轉傳值的幾種方式__.net

來源:互聯網
上載者:User
protected void Button1_Click(object sender, EventArgs e)         {            //使用querystring傳值            //Response.Redirect("b.aspx?username=" + TextBox1.Text.Trim() + "&&pwd=" + TextBox2.Text.Trim());            //使用Session傳值            //Session["username"] = TextBox1.Text.Trim();            //Session["pwd"] = TextBox2.Text.Trim();            //Response.Redirect("b.aspx");            //使用Application傳值            //Application["username"] = TextBox1.Text.Trim();            //Application["pwd"] = TextBox2.Text.Trim();            //Response.Redirect("b.aspx");            //使用Cookie傳值            //HttpCookie hc = new HttpCookie("username",TextBox1.Text.Trim());            //HttpCookie hc2 = new HttpCookie("pwd",TextBox2.Text.Trim());            //Response.AppendCookie(hc);//將建立的Cookie添加到內部Cookie集合中            //Response.AppendCookie(hc2);            //Server.Transfer("b.aspx"); //Cookie需要使用Server.Transfer跳轉            //使用Server.Transfer傳值            //Server.Transfer方法把流程從當前頁面引導到另一個頁面中,新的頁面使用前一個頁面的應答流,所以這個方法是完全面象對象的。            //Server.Transfer("b.aspx"); }         //使用Server.Transfer傳值        //public string GetUserName        //{        //    get { return TextBox1.Text.Trim(); }        //}        //public string GetPwd        //{        //    get { return TextBox2.Text.Trim(); }        //}

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

下面是一個例子:a.aspx的C#代碼

 private void Button1_Click(object sender, System.EventArgs e) { string s_url; s_url = "b.aspx?name=" + Label1.Text; Response.Redirect(s_url); }

b.aspx中C#代碼

private void Page_Load(object sender, EventArgs e) { Label2.Text = Request.QueryString["name"]; }

2. 使用Session變數

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

a.aspx的C#代碼

private void Button1_Click(object sender, System.EventArgs e) { Session["name"] = Label.Text; }

b.aspx中C#代碼

private void Page_Load(object sender, EventArgs e) { string name; name = Session["name"].ToString(); }

 

3. 使用Cookie物件變數

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

a.aspx的C#代碼

private void Button1_Click(object sender, System.EventArgs e) { HttpCookie cookie_name = new HttpCookie("name"); cookie_name.Value = Label1.Text; Reponse.AppendCookie(cookie_name); Server.Transfer("b.aspx"); }


b.aspx中C#代碼 

private void Page_Load(object sender, EventArgs e) { string name; name = Request.Cookie["name"].Value.ToString(); }

 

4. 使用Application 物件變數

Application對象的作用範圍是整個全域,也就是說對所有使用者都有效。其常用的方法用Lock和UnLock。

a.aspx的C#代碼

private void Button1_Click(object sender, System.EventArgs e) { Application["name"] = Label1.Text; Server.Transfer("b.aspx"); }

b.aspx中C#代碼

private void Page_Load(object sender, EventArgs e) { string name; Application.Lock(); name = Application["name"].ToString(); Application.UnLock(); }


 

5. 使用Server.Transfer方法

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

a.aspx的C#代碼

public string Name { get{ return Label1.Text;} } private void Button1_Click(object sender, System.EventArgs e) { Server.Transfer("b.aspx"); }


b.aspx中C#代碼

private void Page_Load(object sender, EventArgs e) { a newWeb; //執行個體a表單 newWeb = (source)Context.Handler; string name; name = newWeb.Name; } 



 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.