asp.net几种页面传值的方式
2013-09-06 23:15
375 查看
1.QueryString()方法
传递的值会显示在浏览器中,可以传递一个或多个值,对于安全要求不高的数据可以采用这种方法,但不支持传递数组和对象
HTML
b.aspx.cs
2.使用Application对象变量
Application对象的作用范围是整个全局,对所有用户都有效。其常用的方法用Lock和UnLock。
a.aspx.cs
b.aspx.cs
3.Session对象变量
Session存储在服务器,作用范围为单用户,过量存储会耗费大量的服务器资源,是最常用的的方法
a.aspx.cs
b.aspx.cs
4.Cookie对象变量
Cookie存于客户端,与Session一样,作用范围为单用户,配合asp.net的Request对象使用
a.aspx.cs
5.Server.Transfer()方法
Session需要服务器返回两次,Server.Transfer()方法需要服务器返回一次,该方法将页面引导到新页面,新页面使用前一个页面的应答流,面向对象。
a.aspx.cs
<%@ Reference Page="~/a.aspx"%><%--注意这里,如果有子目录,请自己添加子目录--%>
b.aspx.cs
传递的值会显示在浏览器中,可以传递一个或多个值,对于安全要求不高的数据可以采用这种方法,但不支持传递数组和对象
HTML
<div> <a href="b.aspx?id=1&&name=text">url传值</a> </div>
b.aspx.cs
protected void Page_Load(object sender, EventArgs e) { string id = Request.QueryString["id"].ToString(); string name = Request.QueryString["name"].ToString(); Response.Write(id + " " + name); }
2.使用Application对象变量
Application对象的作用范围是整个全局,对所有用户都有效。其常用的方法用Lock和UnLock。
a.aspx.cs
protected void Button1_Click(object sender, EventArgs e) { Application["name"] = "Test"; Response.Redirect("b.aspx"); }
b.aspx.cs
protected void Page_Load(object sender, EventArgs e) { Application.Lock(); string name = Application["name"].ToString(); Application.UnLock(); Response.Write(name); }
3.Session对象变量
Session存储在服务器,作用范围为单用户,过量存储会耗费大量的服务器资源,是最常用的的方法
a.aspx.cs
protected void Button1_Click(object sender, EventArgs e) { Session["name"] = "Test"; Response.Redirect("b.aspx"); }
b.aspx.cs
protected void Page_Load(object sender, EventArgs e) { string name = Session["name"].ToString(); Response.Write(name); }
4.Cookie对象变量
Cookie存于客户端,与Session一样,作用范围为单用户,配合asp.net的Request对象使用
a.aspx.cs
protected void Button_Click(object sender, EventArgs e) { HttpCookie cookie_name = new HttpCookie("name"); cookie_name.Value = "TestCookie"; Response.Cookies.Add(cookie_name); Response.Redirect("b.aspx"); }b.aspx.cs
protected void Page_Load(object sender, EventArgs e) { string name = Request.Cookies["name"].Value.ToString(); Response.Write(name); }
5.Server.Transfer()方法
Session需要服务器返回两次,Server.Transfer()方法需要服务器返回一次,该方法将页面引导到新页面,新页面使用前一个页面的应答流,面向对象。
a.aspx.cs
public partial class a : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public string Name { get { return "TestServerTrans"; } } protected void Button1_Click(object sender, EventArgs e) { Server.Transfer("b.aspx"); } }注意:b.aspx页面中第一句加上这句代码
<%@ Reference Page="~/a.aspx"%><%--注意这里,如果有子目录,请自己添加子目录--%>
b.aspx.cs
protected void Page_Load(object sender, EventArgs e) { a newWebform = (a)Context.Handler; string name = a.Name; Response.Write(name); }
相关文章推荐
- 在ASP.NET中页面传值的几种方式(转:具体地址记不清了)
- ASP.NET 页面之间传值的几种方式
- ASP.NET页面间传值的几种方式
- ASP.NET页面传值的几种方式
- ASP.NET页面间传值的几种方式
- asp.net项目页面间传值的几种方式(部分转载)
- 在ASP.NET中页面传值的几种方式(转)
- ASP.NET中-页面传值-的几种方式
- 在ASP.NET中页面传值的几种方式(转:具体地址记不清了)
- asp.net页面跳转传值的几种方式
- ASP.NET页面间传值的几种方式
- ASP.NET页面间传值的几种方式
- ASP.NET:页面传值的几种方式
- Asp.Net页面传值的几种方式
- asp.net页面传值的几种方式
- ASP.NET T页面传值几种方式比较
- asp.net中页面之间小数据量传值的几种方式
- ASP.NET页面间传值的几种方式
- 转:ASP.NET页面传值的几种方式
- ASP.NET中页面传值的几种方式