一、Session的使用
//写入
String name = username.Text;
Session["username"] = name;
//判断
if(Session["username"] != null)
{
//TODO
}
例:通过session传值,输入账号密码,点击登录,
在另外页面显示
点击返回可以查看在原先页面查看到登录的用户名
相关代码:
namespace class3
{
public partial class test1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Session["username"] != null)
{
Label1.Text = "当前登录的用户是:" + Session["username"].ToString();
}
}
//登录按钮
protected void Button1_Click(object sender, EventArgs e)
{
String name = username.Text;
Session["username"] = name;
Response.Redirect("test1show.aspx");
}
}
}
namespace class3
{
public partial class test1show : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String name = "";
if (Session["username"] != null)
{
name = Session["username"].ToString();
}
Label1.Text = "当前登录的用户是:" + name;
}
//页面返回按钮
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("test1.aspx");
}
}
}
二、Application的使用
和Session的用法类似
//写入
Application["reng"] = 0;
例:获取登录的人数,点击退出后人数减少,记录总访问量
相关代码:
namespace class3
{
public partial class test2show : System.Web.UI.Page
{
//初始化用户名
private String name;
//初始化密码
private String pass;
protected void Page_Load(object sender, EventArgs e)
{
//初始化在线人数
int zaixian = (Application["zaixian"] == null) ? 0 : (int)Application["zaixian"];
//初始化总人数
int reng = (Application["reng"] == null) ? 0 : (int)Application["reng"];
//判断是否是第一次提交
Boolean isFirst = (reng == 0) ? true : false;
//请求类型
String method = "";
method = Request.HttpMethod.ToUpper();
//提交name和pass
if (method == "POST")
{
name = Request.Form["username"];
pass = Request.Form["password"];
}
//判断在线人数和总人数
if(name !=null && pass != null)
{
if (isFirst)
{
//初始在线人数为0
Application["zaixian"] = 0;
//初始总人数为0
Application["reng"] = 0;
//登录后
Application["zaixian"] = (int)Application["zaixian"] + 1;
Application["reng"] = (int)Application["reng"] + 1;
zaixian = (int)Application["zaixian"];
reng = (int)Application["reng"];
}
else
{
//登录后
Application["zaixian"] = (int)Application["zaixian"] + 1;
Application["reng"] = (int)Application["reng"] + 1;
zaixian = (int)Application["zaixian"];
reng = (int)Application["reng"];
}
}
Label1.Text = "在线人数:" + zaixian + " 总访问量:" + reng;
}
//退出按钮逻辑
protected void Button1_Click(object sender, EventArgs e)
{
Application["zaixian"] = (int)Application["zaixian"] - 1;
Response.Redirect("test2.aspx");
}
}
}
三、Cookie的使用
//写入
Response.Cookies["username"].Value = name;
//设置过期时间 7天
Response.Cookies["username"].Expires = DateTime.Now.AddDays(7);
例:将登录后的用户名进行记录,存入Cookie并设置过期时间
相关代码:
namespace class3
{
public partial class test3show : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//初始化用户名
String name = "";
//初始口令
String pass = "";
//请求类型
String method = "";
//是否选择存Cookie
Boolean isSave = false;
method = Request.HttpMethod.ToUpper();
//提交name和pass
if (method == "POST")
{
name = Request.Form["username"];
pass = Request.Form["password"];
isSave = (Request.Form["CheckBox1"] == null) ? false : true;
}
//判断是否存入
if (isSave)
{
Response.Cookies["username"].Value = name;
Response.Cookies["username"].Expires = DateTime.Now.AddDays(7);
Response.Cookies["password"].Value = pass;
Response.Cookies["password"].Expires = DateTime.Now.AddDays(7);
}
//输出
Label1.Text = "用户名:" + name + "<br />" +
"口令:" + pass;
}
//返回按钮
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("test3.aspx");
}
}
}
源码下载地址:https://download.youkuaiyun.com/download/dhywjx/10468324