Application、QueryString、session、cookie、ViewState、Server.Transfer等
Application:
WebForm1.aspx:
protected void Button1_Click(object sender, EventArgs e)
{
;
Response.Redirect("WebForm2.aspx"); // 页面跳转。等价于 HttpContext.Current.Response.Redirect("WebForm2.aspx");
}
///////////////////////////////////////////
WebForm2.aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Application["t"] != null)
{
this.Literal1.Text = this.Application["t"].ToString();
int count = (int)this.Application["t"];
count++;
// 加解锁:作用防止大量用户同时访问造成的数据不准确。
this.Application.Lock(); // application加锁
this.Application["t"] = count;
this.Application.UnLock(); // 释放锁
}
}
}
1.容易丢失。例如:代码做了修改重新编译启动。
2.可以多用户同时使用该数据。
Session:
WebForm1.aspx:
protected void Button1_Click(object sender, EventArgs e)
{
// 设置session
this.Session["user"] = "namejr";
Response.Redirect("WebForm2.aspx");
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm2.aspx");
}
///////////////////////////////////////////
WebForm2.aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (this.Session["user"] != null)
{
this.Literal1.Text = this.Session["user"].ToString();
}
else
{
this.Literal1.Text = "未登录";
}
}
}
session的信息是存储在服务器的,但是为了服务器区别出是哪一个用户,会在用户本地产生一个cookie,为了防止用户禁用cookie,可以再web.config做如下设置:
<system.web>
<!-- 开启debug -->
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<!-- 防止用户禁用本地cookie -->
<sessionState cookieless="UseUri"></sessionState>
</system.web>
Cookie:
string a = this.Request.Cookies["a"].Value; // 获取cookie的值
) }); // 添加cookie。等同于HttpContext.Current.Response.AppendCookie(new HttpCookie() { Name = "c", Value = "C", Expires = DateTime.Now.AddDays(1) });
ViewState:
使用ViewState的数据保留在每个单独的页面,不能够跨页面进行实现
;
Server.Transfer 和 Response.Redirect:
// Response.Redirect
protected void Button1_Click(object sender, EventArgs e)
{
// 使用Response.Redirect相当于在客户端进行页面的跳转,具体看导航栏便可知道
Response.Redirect("WebForm2.aspx");
}
// Server.Transfer
protected void Button2_Click(object sender, EventArgs e)
{
// Server.Transfer相当于在服务器完成页面的指向,导航栏现实的仍然是WebForm1.aspx
Server.Transfer("WebForm2.aspx");
}
Application、QueryString、session、cookie、ViewState、Server.Transfer等的更多相关文章
- Asp.net 服务器Application,Session,Cookie,ViewState和Cache区别
2.8 Context 的使用Context 对象包含与当前页面相关的信息,提供对整个上下文的访问,包括请求.响应.以及上文中的Session 和Application 等信息.可以使用此对象在网页之 ...
- Application,Session,Cookie,ViewState和Cache区别
在ASP.NET中,有很多种保存信息的内置对象,如:Application,Session,Cookie,ViewState和Cache等.下面分别介绍它们的用法和区别. 方法 信息量大小 作用域和保 ...
- Web缓存Cache、Application、Session与ViewState
在ASP.NET中,有很多种保存信息的对象.例如:APPlication,Session,Cookie,ViewState和Cache等,那么它们有什么区别呢?每一种对象应用的环境是什么? 方法 信息 ...
- ASP.NET:Application,Session,Cookie,ViewState和Cache之间的区别(转)
在ASP.NET中,有很多种保存信息的对象.例如:Application,Session,Cookie,ViewState和Cache等,那么它们有什么区别呢?每一种对象应用的环境是什么? 为了更清楚 ...
- [区别]APPlication,Session,Cookie,ViewState和Cache
原文发布时间为:2009-08-01 -- 来源于本人的百度文章 [由搬家工具导入] 在ASP.NET中,有很多种保存信息的对象.例如:APPlication,Session,Cookie,ViewS ...
- (转)Application, Session, Cookie, Viewstate, Cache对象用法和区别
================================================================================ 1.Applicati ...
- .NET Application,Session,Cookie,ViewState,Cache对象用法
作用域 保存地址 生命周期Application 应用程序 服务器内存 IIS启动Session 整个站点 服务器内存 Session到时 默认20分钟Cashe 应用程序 服务器内存 应用程序的周期 ...
- APPlication,Session,Cookie,ViewState和Cache之间的区别
1.Application:用于保存所有用户共用的数据信息. 在Asp.Net中类似的配置数据最好保存在Web.config文件中.如果使用Application对象,一个需要考虑的问题是任何写操作都 ...
- Application,Session,Cookie,ViewState,Cache对象用法、作用域的区别
1.Application:用于保存所有用户共用的数据信息.在Asp.Net中类似的配置数据最好保存在Web.config文件中.如果使用Application对象,一个需要考虑的问题是任何写操作都要 ...
- asp.net Server.Transfer
页面跳转传参. 如果不是通用的跳转可以通过,在原始页面定义对象保存数据 跳转的目标页面可以: SourcePage page=(SourcePage)Context.Handler; //获取源页面的 ...
随机推荐
- Web应用增加struts2支持
编辑Web应用的web.xml配置文件,配置Struts2的核心Filter.下面是增加了Struts2的核心 Filter配置的web.xml配置文件的代码片段. <!-- 定义struts2 ...
- Problem B: STL——集合运算
Description 集合的运算就是用给定的集合去指定新的集合.设A和B是集合,则它们的并差交补集分别定义如下: A∪B={x|x∈A∨x∈B} A∩B={x|x∈A∧x∈B} A-B={x|x∈A ...
- oraclesql语句笔记
1. ORA-00947:Not enough values 原因:values没有写足够的值与select()中的字段对应 2.查看一张表中共有多少个字段 select count(*) from ...
- Qt代码
ui->LoginPushButton->setStyleSheet(//正常状态样式 "QPushButton{" "background-color:rg ...
- Selenium常用API详解介绍
转至元数据结尾 由 黄从建创建, 最后修改于一月 21, 2019 转至元数据起始 一.selenium元素定位 1.selenium定位方法 2.定位方法的用法 二.控制浏览器操作 1.控制 ...
- php优秀框架codeigniter学习系列——CI_Input类学习
这篇文章主要介绍CI核心框架工具类CI_Input. 根据CI文档自己的定义,该类用来: 提前处理全局变量,以保证安全; 提供一些帮助函数用来处理输入数据. 以下选取类中的重点方法进行说明. __co ...
- L2-007 家庭房产 (25 分)
L2-007 家庭房产 (25 分) 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(≤),随后N行,每行按下 ...
- R语言 一套内容 从入门 到放弃
[怪毛匠子整理] 1.下载 wget http://mirror.bjtu.edu.cn/cran/src/base/R-3/R-3.0.1.tar.gz 2.解压: tar -zxvf R-3.0. ...
- Notepad++的实用技巧
一. 安装notepad + + notepad++的下载.安装非常easy.下一步下一步,所有选项都默认就可以安装好.但有几点需要注意. 截止到写这篇博文,notepad++的最新版本为7.5 ...
- element-ui隐藏组件scrollbar的使用
话不多说,直接上图 总结:el-scrollbar组件设置高度100%包裹住需要滚动的dom结构即可. 再例如: 至于配置props,参见源码https://github.com/ElemeFE/el ...