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 }
时间: 2024-10-06 16:30:37