访问会话对象 [英] Accessing Session Object

查看:63
本文介绍了访问会话对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从类中访问Session值.

示例:

How to access Session value from class.

Example:

public class a
{
    int a=10;
}
public static class b
{
    int b=10;
}



表示如何访问这些值类ab
请让我深入解释Session吗?

在此先感谢



Means how can I access these value class a and b value
Please let me explain about Session in depth?

Thanks in advance

推荐答案

您可以使用静态属性

You may use Static Properties

public class TestClass
{
    public static int  PropertyA {

        set{ HttpContext.Current.Session["objA"] = value;}

        get{
                return(HttpContext.Current.Session["objA"]==null
                ?0:int.parse(HttpContext.Current.Session["objA"].ToString())
                       );
            }
                         }
}



您可以使用此属性在会话中轻松获得和设置.




You Can get and set easily in session using this Property.

as In

TestClass.PropertyA=10;



它将在Session中保存值,可以使用相同的属性进行检索

i-e



It will save value in Session which can be retrieved using same property

i-e

Response.Write(TestClass.PropertyA.ToString());





//对于布尔变量.

Hi,


//For Boolean Variable.

Session("Flag") = true; //Assign Value For Session Variable

if ((bool)(Session("Flag")))
{
    //do something...
}




您可以用相同的方式使用For String以及Datatable,Dataset等对象.

希望这对您有帮助...




In The Same Way You can use For String as well as objects Like Datatable,Dataset,etc.

Hope This May help You...


遵循Hammad Abbasi的答案.这是从一个类到另一个类访问会话值的正确方法
follow the Hammad Abbasi answer.this is right way to access the session value from one class to another


这篇关于访问会话对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆