I have the following global variable:
private ArrayList listSelectedUnavailables
{
get
{
return (ArrayList)ViewState["listSelectedUnavailables"];
}
set
{
ViewState["listSelectedUnavailables"] = value;
}
}
I can work with it in every single procedure of the webform.
However, I need to use it in a WebMethod that I have in the same WebForm, but it seems not to identify any of the global variables. So:
How can I access a global variable from a WebMethod?
A ViewState
property depends on having a page (.aspx) post back a view state, that's where your "variable" is stored. A WebMethod
does not include the full page postback (if any post back at all) and so there is no view state for it to read from. Instead you may want to use a session variable like:
private ArrayList listSelectedUnavailables
{
get
{
return (ArrayList)Session["listSelectedUnavailables"];
}
set
{
Session["listSelectedUnavailables"] = value;
}
}
The session stores the variable in the web server's memory (but related to a specific browser session). This has it's own draw-backs, such as being volatile to a worker process reset, load balancing cosiderations, etc.