c# - How to store Data from static WebMethods into the ViewState -


now have done research. need store data have retrieved ajax call webmethod on page place can pull again anytime.

i thought @ first viewstate best option. unfortunately cannot reference in same way can in non-static methods. if make instance of page store in viewstate, believe de-instantiated @ end of method destroying whatever data saved.

i need data purpose of database calls doing in other webmethods.

the basic method in c# codebehind aspx page looks this:

    [webmethod]     [scriptmethod]     public static string populatemodels(string[] makeids)     {      } 

so example need save selected makes pull future database calls. since of boxes cascade in terms of filtering , pulling database.

update:

this code works retrieving , storing data in 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"];      } 

as joe enos pointed out, viewstate part of page instance, can use session cache, this:

[webmethod(enablesession = true)] [scriptmethod] public static string populatemodels(string[] makeids) {     // check if value in session     if(httpcontext.current.session["supersecret"] != null)     {         // getting value out of session         var supersecretvalue = httpcontext.current.session["supersecret"].tostring();     }      // storing value in session     httpcontext.current.session["supersecret"] = mysupersecretvalue; } 

note: allow use part of page asp.net ajax page methods or store values server, while allow page postbacks have access data via session well.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -