. NET common tool class--session operation class

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;

Namespace ZC. Utils
{
<summary>
<para> </para>
Common tool class--session operation class
<para>----------------------------------------------------------</para>
<para> Addsession: Add session, valid for default </para>
<para> Addsession: Add session and adjust the validity period to minutes or years </para>
<para> GetSession: Reads a Session object value </para>
<para> Delsession: Delete a Session object </para>
</summary>
public class Session
{

public Object This[string Key]
{
Get
{
return Get (key);
}
Set
{
Set (key, value, 60, 0);
}
}



#region Add session, valid for default
<summary>
Add session, valid for default
</summary>
<param name= "Strsessionname" >session object name </param>
<param name= "strvalue" >session value </param>
public static void Add (String strsessionname, Object ObjValue)
{
Httpcontext.current.session[strsessionname] = ObjValue;
}
#endregion

#region Add session and adjust the validity period to minutes or years
<summary>
Add a session and adjust the validity period to minutes or years
</summary>
<param name= "Strsessionname" >session object name </param>
<param name= "strvalue" >session value </param>
<param name= "Iexpires" > minutes: Greater than 0 is valid for the number of minutes, equals 0 is valid for the following year </param>
<param name= "Iyear" > Years: When the number of minutes is 0 years is the validity period, when the number of minutes is greater than 0 o'clock this parameter is set at random </param>
public static void Set (String strsessionname, Object objvalue, int iexpires, int iyear)
{
Httpcontext.current.session[strsessionname] = ObjValue;
if (Iexpires > 0)
{
HttpContext.Current.Session.Timeout = Iexpires;
}
else if (iyear>0)
{
HttpContext.Current.Session.Timeout = * 365 * iyear;
}
}
#endregion

#region reading a Session object value
<summary>
Reading a Session object value
</summary>
<param name= "Strsessionname" >session object name </param>
<returns>session Object Values </returns>
public static Object Get (String strsessionname)
{
return Httpcontext.current.session[strsessionname];
}
#endregion

#region Delete a Session object
<summary>
Delete a Session object
</summary>
<param name= "Strsessionname" >session object name </param>
public static void Remove (String strsessionname)
{
HttpContext.Current.Session.Remove (Strsessionname);
}
#endregion
}
}

. NET common tool class--session operation class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.