不同页面之间如何传值?我们假设A和B两个页面,A是传递,B是接收。
下面学习4种方式:
- 通过URL链接地址传递
- POST方式传递
- Session方式传递
- 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 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> 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"]