5

Now I have done some research. I need to store some data that I have retrieved from an ajax call to my WebMethod on my page into some place where I can pull it back again anytime.

I thought at first that the ViewState would be the best option. Unfortunately you cannot reference it in the same way you can in non-static methods. Even if I make instance of the page to store it in the ViewState, I believe that it would be de-instantiated at the end of the method destroying whatever data I saved.

I need this data for the purpose of database calls that I am doing in other WebMethods.

The basic method in my C# codebehind for my aspx page looks like this:

    [WebMethod]
    [ScriptMethod]
    public static string populateModels(string[] makeIds)
    {

    }

So for example I need to save the selected makes to pull from for future database calls. Since most of my boxes cascade in terms of filtering and pulling from the database.

Update:

This code works for retrieving and storing data in the SessionState in static WebMethods.

    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public static string populateYears(string[] modelIds)
    {

        HttpContext.Current.Session["SelectedModels"] = modelIds;

        string[] makeids = (string[])HttpContext.Current.Session["SelectedMakes"];
     }
Alexander Ryan Baggett
  • 2,347
  • 4
  • 34
  • 61

2 Answers2

7

As Joe Enos pointed out, ViewState is part of the page instance, but you can use Session cache, like this:

[WebMethod(EnableSession = true)]
[ScriptMethod]
public static string populateModels(string[] makeIds)
{
    // Check if value is in Session
    if(HttpContext.Current.Session["SuperSecret"] != null)
    {
        // Getting the value out of Session
        var superSecretValue = HttpContext.Current.Session["SuperSecret"].ToString();
    }

    // Storing the value in Session
    HttpContext.Current.Session["SuperSecret"] = mySuperSecretValue;
}

Note: This will also allow you to use part of your page with ASP.NET AJAX Page Methods to just get or store some values to the server, while also allow your page postbacks to have access to the data via Session as well.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • This does not work as I thought it would. It still me tells "An object reference is required for the nonstatic field method or property 'System.web.ui.Session.get' " My non-working code now looks like this: [WebMethod(EnableSession=true)] [ScriptMethod] public static string populateModels(string[] makeIds) { Session["SelectedMakes"] = makeIds; } – Alexander Ryan Baggett Aug 23 '13 at 17:43
  • @AlexanderRyanBaggett - ah yes, sorry forgot you have grab the `Session` out of the `HttpContext.Current`, answer updated to reflect this. – Karl Anderson Aug 23 '13 at 17:48
  • Yeah I was looking at that here. http://stackoverflow.com/questions/2577183/how-can-i-get-the-value-of-a-session-variable-inside-a-static-method I can assign a string[] to it, but I can't seem to pull a string[] from it. It's talking about casting it when I try to retrieve it. – Alexander Ryan Baggett Aug 23 '13 at 18:01
  • Basically I need a way to convert it to a string array or a way to access individual elements as strings. – Alexander Ryan Baggett Aug 23 '13 at 18:08
  • @AlexanderRyanBaggett - sorry I was away for a bit, glad you solved your issue. Good luck to you. :-) – Karl Anderson Aug 23 '13 at 19:03
6

ViewState is a property of the page, which runs through the ASP.NET WebForms page lifecycle. Using WebMethods with AJAX skips the entire page lifecycle, and skips ViewState altogether.

So you won't be able to use ViewState the way you're looking. In order to use AJAX and still have access to all the WebForms stuff like ViewState and control properties, you need to use UpdatePanels.

You'll need to find alternatives - for example, instead of ViewState, you can put things into hidden fields, then use javascript to read and populate those hidden fields. If you do this, you can read and write to these fields from both the javascript and ASP.NET worlds.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • Interesting thoughts there. Hidden fields, I have heard about that before, but never used it in practice. – Alexander Ryan Baggett Aug 23 '13 at 17:33
  • @AlexanderRyanBaggett If you go outside of the WebForms world, you'll see hidden fields used a lot more often. They're great for MVC, for example where you need to post a full model including its ID, but don't want the ID to be visible like the rest of the properties in input fields. Just keep in mind that they're not secret at all - not encrypted, not even encoded, and can be easily manipulated by the user - unlike ViewState, which is always validated to help prevent simple tampering, and often server-encrypted. – Joe Enos Aug 23 '13 at 17:38
  • Thank you for the advice. My brother who works in Ecommerce said that they used hidden fields where he worked. – Alexander Ryan Baggett Aug 23 '13 at 17:50