使用会话的问题-“需要对象引用" [英] Problem using sessions - "An object reference is required"

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

问题描述

我有一个按钮,当我单击此方法时(在JS-ajax之后)将被执行:

I have a button that when I click this method is executed (after JS - ajax):

[System.Web.Services.WebMethod]
   public static string carregarNotas()
   {
       try
       {
           string id;
           id = Session["weeksLength"].ToString();
           string user = Session["username"].ToString();

//.........



问题是,我得到需要对象引用".如果我将id = ...放在page_load上,则不会出现此问题.

我怎么解决这个问题?我在这种方法中执行一条SQL语句,并且需要该会话变量.



The problem is, I get "An object reference is required". If I place that id = ... on page_load I dont get this problem.

How can I solve this problem? I execute an SQL statement in this method and I need that session variable.

thanks in advance.

推荐答案

阅读文档通常会有所帮助.

http://msdn.microsoft.com/en-us/library/ykshe9s9.aspx [ ^ ]

参数

enableSession
类型:System.Boolean
初始化是否为XML Web服务方法启用会话状态.

没有此会话对象将无效.
It usually helps when you read the documentation.

http://msdn.microsoft.com/en-us/library/ykshe9s9.aspx[^]

Parameters

enableSession
Type: System.Boolean
Initializes whether session state is enabled for the XML Web service method.

Without this the Session object is not valid.


正如我所说,我自己的回答和解释是:

问题在于Session对象是我正在使用的Page/Control类的非静态成员.

所以我必须使用:

As I said, my own answer and the explanation:

The problem is that the Session object is a non static member of the Page/Control class that I was using.

So I have to use:

static void testStaticFunc()
{
  HttpContext.Current.Session["test"] = "testString";
}




而且有效.

还是谢谢你.




And it works.

Thanks anyway.


这篇关于使用会话的问题-“需要对象引用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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