5-12 session对象实例

效果:

(一)发送页面

1、将在TextBox控件中输入的字符串保存至Session对象中;

2、将在TextBox控件中输入的数组保存至Session对象中;


3、将在日历表中选中的对象保存至Session对象中;


4、用SessionAdd方法对重名的Key进行设置(目标位第一个控件),刷新页面后第一个控件值被第四个覆盖掉;


5、点击超链接,跳转页面;

(二)读取页面(read页面,所有代码都写在PageLoad事件当中)

1、读取第一个文本框内容:


2、获取数组:



3、获取控件:


4、遍历session:

Label1.Text=" ";

foreach(string _key in Session.Keys)

{Label1.Text+=("["+_key+"]&nbsp;@nbsp;"+Session[_key].GetType().ToString()+"<br/>")

5、获取SessionID:

Label2.Text=Session.SessionID.ToString();

6、获取Session的剩余时间:

Label3.Text=Session.Timeout.ToString();

7、删除全部session:

protected void Button1_Click(object sender, EventArgs e)

{

    Session.Abandon();

    Response.Redirect(Request.Url.LocalPath.ToString());//通过该方法,可以直接

}

8、删除单个session:

protected void Button2_Click(object sender, EventArgs e)

{

    Session.Remove("ss_txt");

    Response.Redirect(Request.Url.LocalPath.ToString());

}