質問

Ok, I'm a little stuck on this one. I think my problem lies with the life cycle of the page but I'm not sure nor am I sure how to fix the problem.

I have a static method that needs access to the Session. However, this method gets called in the middle of an AJAX call. The code looks like this:

$(object).click(function() {
    Library.Ajax.GetData(someParameter, function(results) {
         //do call back code
    });
});

The web method:

[WebMethod]
public List<string> GetData(string parameter)
{
      return new Library.Class().GetData(parameter);
}

The C# Class:

public List<string> GetData(string parameter)
{
     //working with parameter & stuff
     CallStaticMethod();
     //return things
}

The static method:

 public static CustomClass CallStaticMethod()
 {
    var data = HttpContext.Current.Session["variable"];
    //do other things
 }

On page load, the Session has data. When this static method is called, the page has not posted back or reloaded. However, placing a break point within each method call shows that HttpContext.Current.Session is null at the time they are being called.

I'm admittedly terrible when it comes to understanding the page life cycle. Can anyone shed some light on what is going on here? And what I can do to solve the problem?

役に立ちましたか?

解決

how about?

[WebMethod(EnableSession = true)]
public List<string> GetData(string parameter)

MSDN::How to: Use the WebMethod Attribute

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top