頁值傳遞是ASP學習早期都會遇到的一個問題,一般來說,它包括頁值傳遞、存儲對象值傳遞等等,那么匯總ASP.NET 頁面之間傳遞的方法大家清楚嗎?下面就讓錯新技術頻道小編帶你來學習一下吧!
1、通過URL鏈接地址傳遞
(1) send.asp代碼
復制代碼 代碼如下:
protected void Button1_Click(object sender, EventArgs e)
{
Request.Redirect("Default2.aspx?username=honge");
}
(2) receive.aspx代碼
?
復制代碼 代碼如下:
?
string username = Request.QueryString["username"];//這樣可以得到參數值。
2、POST方式傳遞
(1) send.asp代碼
?
復制代碼 代碼如下:
?
<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>
(2) receive.aspx代碼
?
復制代碼 代碼如下:
?
string username = Ruquest.Form["receive"];
3、Session方式傳遞
(1) send.asp代碼
?
復制代碼 代碼如下:
?
protected void Button1_Click(object sender, EventArgs e)
{
Session["username"] = "honge";
Request.Redirect("Default2.aspx");
}
(2) receive.aspx代碼
?
復制代碼 代碼如下:
?
string username = Session["username"];//這樣可以得到參數值。
4、Application方式傳遞
(1) send.asp代碼
?
復制代碼 代碼如下:
?
protected void Button1_Click(object sender, EventArgs e)
{
Application["username"] = "honge";
Request.Redirect("Default2.aspx");
}
(2) receive.aspx代碼
?
復制代碼 代碼如下:
?
string username = Application["username"];這樣可以得到參數值。
5、使用Server.Transfer進行傳遞
(1) send.asp代碼
?
復制代碼 代碼如下:
?
public string Name
{
get {
return "honge";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("Default2.aspx");
}
(2) receive.aspx代碼
?
復制代碼 代碼如下:
?
send d = Context.Handler as send ;
if (d != null)
{
Response.Write(d.Name);//這樣可以得到參數值。
}
上文是錯新技術頻道小編為大家介紹的?匯總ASP.NET 頁面之間傳遞的方法,希望對你學習這方面有幫助,也希望大家繼續支持錯新技術頻道!