cation 变量赋值
Application["Name"] = txtName.Text; Response.Redirect("WebForm5.aspx");
// 从 Application 变量中取出值
if( Application["Name"] != null ) Label3.Text = Application["Name"].ToString();
5. Server.Transfer 方式(或称 HttpContext 方式) 方式( 方式)
我们还可以使用 Server.Transfer 方式(或称 HttpContext 方式)在页面之间传递变量,此 时,要传递的变量可以通过属性或方法来获得,使用属性将会比较容易一些.好,让我们在第一 个页面中来写一个用来获得 TextBox 值的属性:
public string GetName { get { return txtName.Text; } }
我们需要使用 Server.Transfer 把这个值发送到另外一个页面中去,请注意 Server.Transfer 只是发送控件到一个新的页面去,而并不会使浏览器重定向到另一个页面.所以,我们我们在地 址栏中仍然看到的是原来页面的 URL.如下代码所示:
Server.Transfer("WebForm5.aspx");
接下来,我们到"WebForm5.aspx"看看:
// You can declare this Globally or in any event you like
WebForm4 w;
// Gets the Page.Context which is Associated with this page
w = (WebForm4)Context.Handler; // Assign the Label control with the property "GetName" which return s string
Label3.Text = w.GetName;
结束语: 结束语:
如我们看到的那样,各种传值方式都各有优劣,在不同的情况下选择适当的方式是很重要的.