aspx頁面傳值常用幾種方式

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   使用   sp   

Redirect
 1 page1.aspx 2 protected void Button1_Click(object sender, EventArgs e) 3     { 4         Response.Redirect("page2.aspx?name=" + TextBox1.Text); 5     } 6 page2.aspx 7 if (Request.QueryString["name"] != null) 8         { 9             TextBox1.Text = Request.QueryString["name"];10         } 
Cookies
 1 protected void Button1_Click(object sender, EventArgs e) 2     { 3         //Cookies由伺服器端建立,但儲存在用戶端 4         HttpCookie cName = new HttpCookie("Name"); 5         cName.Value = TextBox1.Text; 6         Response.Cookies.Add(cName); 7         Response.Redirect("page2.aspx"); 8     } 9  10 protected void Page_Load(object sender, EventArgs e)11     {12         if (Request.Cookies["Name"] != null)13         {14             TextBox1.Text = Request.Cookies["Name"].Value;15         }16     } 
Session變數:使用者向伺服器首次發送請求被建立,而在瀏覽器關閉或異常發生時終止(還存在到期的情況)
 1 protected void Button1_Click(object sender, EventArgs e) 2     { 3         Session["ses"] = TextBox1.Text; 4         Response.Redirect("page2.aspx"); 5     }  6 protected void Page_Load(object sender, EventArgs e) 7     { 8         if(Session["ses"]!=null) 9         {10             TextBox1.Text = Session["ses"].ToString();11         }12     }
 Application變數:能夠在所有的頁面中訪問,這時候我們可以使用Application變數
 1  protected void Button1_Click(object sender, EventArgs e) 2     { 3         Application["app"] = TextBox1.Text; 4         Response.Redirect("page2.aspx"); 5     }  6 protected void Page_Load(object sender, EventArgs e) 7     { 8         if (Application["app"] != null) 9         {10             TextBox1.Text = Application["app"].ToString();11         }12     } 
Server.Transfer:瀏覽器地址不會改變實際上當您調用 Server.Transfer 時,ASP.NET 將內部調用 Server.Execute 方法來傳輸該的控制項,並調用 Response.End 方法以結束當 前頁的處理。 Response.End 結束執行頁,並調用 Thread.Abort 方法。 Thread.Abort 方法導致 ThreadAbortException 錯誤資訊顯示
 1 public string Name { get { return this.TextBox1.Text.Trim().ToString(); } } 2     protected void Button1_Click(object sender, EventArgs e) 3     { 4         Server.Transfer("page2.aspx"); 5     }  6   7 --在page2.aspx頁面引入:<%@ Reference Page="~/page1.aspx" %> 8 protected void Page_Load(object sender, EventArgs e) 9     {10         page1 p = (page1)Context.Handler;11         //TextBox1.Text = p.Name;這樣也可以12         TextBox1.Text = ((TextBox)p.FindControl("TextBox1")).Text;13     } 

 

aspx頁面傳值常用幾種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.