ASP.NET 不同页面之间传值

不同页面之间如何传值?我们假设A和B两个页面,A是传递,B是接收。

下面学习4种方式:

  1. 通过URL链接地址传递
  2. POST方式传递
  3. Session方式传递
  4. Application方式传递

1. 通过URL链接地址传递

A:
protected void Button1_Click(object sender, EventArgs e)
{
  Request.Redirect("Default2.aspx?username=honge");
}

B:
string username = Request.QueryString["username"];

2. POST方式传递

A:
<form  runat="server" action="receive.aspx" method=post>
  <div>
    <asp:Button  runat="server" OnClick="Button1_Click" Text="Button" />
    <asp:TextBox  runat="server"></asp:TextBox>
  </div>
</form>

B:
string username = Ruquest.Form["receive"];

3. Session方式传递

A:
protected void Button1_Click(object sender, EventArgs e)
{
  Session["username"] = "传递";
  Request.Redirect("Default2.aspx");
}

B:
string username = Session["username"];

4. Application方式传递

A:
protected void Button1_Click(object sender, EventArgs e)
{
  Application["username"] = "传递";
  Request.Redirect("Default2.aspx");
}

B:
string username = Application["username"]