手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#程序中session的基本设置示例及清除session的方法
C#程序中session的基本设置示例及清除session的方法
摘要:session的基本设置:usingSystem;usingSystem.Collections.Generic;usingSystem.T...

session的基本设置:

using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Web.SessionState; namespace OAFrameWork { public class CSession { public static object Get(string Key) { return HttpContext.Current.Session[Key]; } public static string GetString(string Key) { object obj = HttpContext.Current.Session[Key]; if (obj == null) return ""; else return obj.ToString(); } public static object Get(string Key,object DefaultValue) { if (HttpContext.Current.Session[Key] == null) return DefaultValue; else return HttpContext.Current.Session[Key]; } public static object Get(string Key, object DefaultValue,Boolean CanAdd) { if (HttpContext.Current.Session[Key] == null) { if(CanAdd==true) HttpContext.Current.Session.Add(Key, DefaultValue); return DefaultValue; } else return HttpContext.Current.Session[Key]; } public static Boolean Set(string Key,object Value) { try { if (Value == null && HttpContext.Current.Session[Key] != null) { HttpContext.Current.Session.Remove(Key); } else if (HttpContext.Current.Session[Key] == null) { HttpContext.Current.Session.Add(Key, Value); } else { HttpContext.Current.Session[Key] = Value; } return true; } catch (Exception ex) { CMsgBox.Show(ex.Message); return false; } } } }

清除Session:

Session.Abandon();//清除全部Session //清除某个Session Session["UserName"] = null; Session.Remove("UserName");

【C#程序中session的基本设置示例及清除session的方法】相关文章:

C# 屏蔽关键字的实现方法

C#灰度化图像的实例代码

C#.NET字符串比较中忽略符号的方法

C#中执行批处理文件(*.bat)的方法代码

C#生成sitemap站点地图的方法

C# 设置系统日期格式的方法

c#生成缩略图的实现方法

C#中Array与ArrayList用法及转换的方法

使用@符号让C#中的保留字做变量名的方法详解

C#中控制远程计算机的服务的方法

精品推荐
分类导航