using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace WebApplication1{ public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //获取传递过来的那么 if (Session["name"] == null)// “” { Response.Write("你还没有登录,请登录后在进入"); Response.End();//终止 } else { Response.Write("欢迎" + Session["name"] + "登录"); } } protected void Button1_Click(object sender, EventArgs e) { Session.Clear();//清除记录 Response.Redirect("Default.aspx"); } }}
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace WebApplication1{ public partial class Demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //session会话对象 //浏览器与服务器的一次连接是一个会话 string i= Session.SessionID; Response.Write("会话ID"+i); //session主要用来存放会话者的数据的 //session存放服务器内存中的,当前会话者拥有,绘画结束时对象释放 Session["name"] = TextBox1.Text;//往session存放一个数据 Session.Timeout = 1;//超过时间,默认值是20分钟 } protected void Button2_Click(object sender, EventArgs e) { Response.Write("用户名:"+Session["name"]); } }}