asp.net几种页面传值的方式
来源:互联网 发布:tgp网络出口不稳定 编辑:程序博客网 时间:2024/06/05 00:16
1.QueryString()方法
传递的值会显示在浏览器中,可以传递一个或多个值,对于安全要求不高的数据可以采用这种方法,但不支持传递数组和对象
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); }
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 页面之间传值的几种方式
- ASP.NET 页面之间传值的几种方式
- Asp.net C#页面传参的几种方式
- 二维数组
- 设计模式中迷宫的实现,用于学习创建型模式
- JobTracker任务调度器之JobQueueTaskScheduler
- c# 给一个datatime型值 加减
- 在vs2010中检测 C++ 程序内存泄露
- asp.net几种页面传值的方式
- Job的提交——JobTracker
- Job的提交—客户端
- linux小记6 权限
- Hadoop公平调度器算法解析
- 排序算法总结(一)计数排序
- 编译mongodb C++驱动 windows
- Hadoop中Map任务的执行框架
- SSH+Highcharts 生成动态图表 ——在思考中解决问题