suppose i have one static method and i need to access viewstate from that method…how could i do so…i know it is not possible but there must be some way out.
[WebMethod]
public static string GetData(int CustomerID)
{
string outputToReturn = "";
ViewState["MyVal"]="Hello";
return outputToReturn;
}
You can get the reference to the page via HttpContext.CurrentHandler. But since
Control.ViewStateis protected you can’t access it (without using reflection) as opposed to theSessionwhich is accessible viaHttpContext.Current.Session.So either don’t use a static method, use the
Sessionor use this reflection approach:However, this won’t work if called via WebService, because then it’s outside of Page-Lifecycle.