asp.net页面间传值的几种方法
发布日期:2021-10-12 02:13:41
浏览次数:2
分类:技术文章
本文共 3819 字,大约阅读时间需要 12 分钟。
表单提交
传送页面代码
表单提交
接收页面代码
protected void Page_Load(object sender, EventArgs e) { string a = Request.Form["SourceData2"].ToString(); txt1.Value = a; }
QueryString传值
传送页面代码
QueryString
protected void btn1_ServerClick(object sender, EventArgs e) { string aa = txt1.Value; string bb = txt2.Value; string url = "DestinationPage5.aspx?parameter1=" + aa + "¶meter2=" + bb; Response.Redirect(url, false); }
接收页面代码
protected void Page_Load(object sender, EventArgs e) { txt1.Value = Request.QueryString["parameter1"].ToString(); txt2.Value = Request.QueryString["parameter2"].ToString(); }
链接地址传值
传送页面代码
接收页面代码
protected void Page_Load(object sender, EventArgs e) { txt1.Value = Request["param1"]; txt2.Value = Request["param2"]; }
Context传值
通过Context传值,在传送页面之前,将需要传递到其他页面的值存在Context中。
传送页面代码
Context
protected void btn1_ServerClick(object sender, EventArgs e) { Context.Items["value"] = txt1.Value; Server.Transfer("DestinationPage3.aspx"); }
接收页面代码
protected void Page_Load(object sender, EventArgs e) { string a = Context.Items["value"].ToString(); txt1.Value = a; }
Server.Transfer传值
传送页面代码
Server.Transfer
protected void btn1_ServerClick(object sender, EventArgs e) { Server.Transfer("DestinationPage4.aspx"); } public string Value { get { return txt1.Value; } }
接收页面代码
public partial class Transfer_DestinationPage4 : System.Web.UI.Page { private Transfer_SourcePage4 sourcePage; protected void Page_Load(object sender, EventArgs e) { sourcePage = (Transfer_SourcePage4)Context.Handler; string aa = sourcePage.Value; txt1.Value = aa; } }
Cookie传值
传送页面代码
protected void btn1_ServerClick(object sender, EventArgs e) { string aa = txt1.Value; HttpCookie cookie = new HttpCookie("MyCookie", aa); Response.Cookies.Add(cookie); string url = "DestinationPage8.aspx"; Response.Redirect(url, false); }Cookie
接收页面代码
protected void Page_Load(object sender, EventArgs e) { HttpCookie myCookie = Request.Cookies["MyCookie"]; txt1.Value = myCookie.Value; }
Session传值
通过Session取值,在一个页面中赋值,在其他页面中共享。为避免造成Session值的混乱无序,应尽量少用Session传非公共的变量。Session比较适合用来保存一些公共变量。
传送页面代码
protected void btn1_ServerClick(object sender, EventArgs e) { Session["value"] = txt1.Value; Response.Redirect("DestinationPage2.aspx"); }Session传值
接收页面代码
protected void Page_Load(object sender, EventArgs e) { txt1.Value = Session["value"].ToString(); }
Application传值
此种方法不常使用,因为Application在一个应用程序域范围共享,所有用户可以改变及设置其值,故只应用于计数器等需要全局变量的地方。
传送页面代码
protected void btn1_ServerClick(object sender, EventArgs e) { string aa = txt1.Value; Application["param1"] = aa; string url = "DestinationPage7.aspx"; Response.Redirect(url, false); }Application
接收页面代码
protected void Page_Load(object sender, EventArgs e) { txt1.Value = Application["param1"].ToString(); }
http://www.cnblogs.com/xiaoyusmile/archive/2012/03/20/2408797.html
转载地址:https://blog.csdn.net/qq_36747842/article/details/78573483 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月24日 11时16分28秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
暴力修改SElinux权限
2021-06-30
camera-open-log
2021-06-30
让这个该死的服务跑起来了~
2021-06-30
LinuxGPIO操作和MTK平台GPIO
2021-06-30
Linux cpu亲和力
2021-06-30
C语言指定初始化器解析及其应用
2021-06-30
安卓9.0添加服务修改SELinux
2021-06-30
深入理解Linux内核链表
2019-04-27
Linux一定需要文件系统吗?
2019-04-27
手机是怎么确定位置信息的?
2019-04-27
使用Adobe Audition生成基本音频
2019-04-27
思必驰算法库录音日志~
2019-04-27
苹果uwb定位技术
2019-04-27
为什么需要超出48K的音频采样率,以及PCM到DSD的演进
2019-04-27
广东,就是这么横?
2019-04-27
手机可以连接多少个蓝牙设备?
2019-04-27
10大黑客专用的 Linux 操作系统,你了解哪些?
2019-04-27
你为什么喜欢VIM?
2019-04-27
RTT大牛告诉你,混合微内核是什么?
2019-04-27
ARM 原子操作里的两个汇编指令
2019-04-27