• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#程序中session的基本设置示例及清除session的方法

C#程序中session的基本设置示例及清除session的方法

作者:sunzhenlin2008 字体:[增加 减小] 来源:互联网 时间:2017-05-28

sunzhenlin2008 通过本文主要向大家介绍了马桶c的个人空间,c语言,欲情 c max,维生素c,奔驰c200等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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;
      }

    }
 

      
    
  }
}

</div>

清除Session:

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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#利用ReportViewer生成报表
  • C#基于正则去掉注释的方法示例
  • C#中new的用法及与override的区别分析
  • C#实现两个richtextbox控件滚动条同步滚动的简单方法
  • C# for循环的经典案例集锦
  • C#操作word的方法示例
  • C#使用WebClient登录网站并抓取登录后的网页信息实现方法
  • C# WinForm制作异形窗体与控件的方法
  • C#实现Excel表数据导入Sql Server数据库中的方法
  • C#使用NPOI上传excel

相关文章

  • 2017-05-28C#中载入界面的常用方法
  • 2017-05-28Winform动态加载TabControl用法实例
  • 2017-05-28使用windows控制台调试服务的方法
  • 2017-05-28C# Stream 和 byte[] 之间的转换
  • 2017-05-28C#中的值传递和引用传递详细解析
  • 2017-05-28C# 基础入门--常量
  • 2017-05-28C#多线程传递参数及任务用法示例
  • 2017-05-28C#实现任务栏通知窗口
  • 2017-05-28C#使用NPOI导入Excel的方法详解
  • 2017-05-28C#对XML文件的各种操作实现方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#中的is和as操作符区别小结
    • C#创建数据库及导入sql脚本的方法
    • C#线程池操作方法
    • c# 图片加密解密的实例代码
    • C#加密在实际中的应用
    • c#中的泛型委托详解
    • c#集合快速排序类实现代码分享
    • C#浮点数的表示和基本运算
    • .NET企业级项目中遇到的国际化问题和解决方法
    • 获取wince mac地址与IP地址解决方案

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有