Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将静态WebMethods中的数据存储到ViewState中_C#_Asp.net_Web Services_Viewstate_Webmethod - Fatal编程技术网

C# 如何将静态WebMethods中的数据存储到ViewState中

C# 如何将静态WebMethods中的数据存储到ViewState中,c#,asp.net,web-services,viewstate,webmethod,C#,Asp.net,Web Services,Viewstate,Webmethod,现在我做了一些研究。我需要将从页面上对WebMethod的ajax调用中检索到的一些数据存储到某个地方,在那里我可以随时将其拉回来 起初我认为ViewState是最好的选择。不幸的是,您不能像在非静态方法中那样引用它。即使我创建了页面的实例来将其存储在ViewState中,我相信它会在方法结束时被反实例化,从而破坏我保存的任何数据 我需要这些数据用于我在其他WebMethods中进行的数据库调用 [WebMethod(EnableSession = true)] [ScriptM

现在我做了一些研究。我需要将从页面上对WebMethod的ajax调用中检索到的一些数据存储到某个地方,在那里我可以随时将其拉回来

起初我认为ViewState是最好的选择。不幸的是,您不能像在非静态方法中那样引用它。即使我创建了页面的实例来将其存储在ViewState中,我相信它会在方法结束时被反实例化,从而破坏我保存的任何数据

我需要这些数据用于我在其他WebMethods中进行的数据库调用

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

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

        string[] makeids = (string[])HttpContext.Current.Session["SelectedMakes"];
     }
我的aspx页面的C#codebehind中的基本方法如下所示:

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

    }
[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;
}
因此,例如,我需要保存选定的make,以便将来调用数据库。因为我的大多数盒子都是从数据库中过滤和提取的

更新: 此代码用于在静态WebMethods中检索和存储SessionState中的数据

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

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

        string[] makeids = (string[])HttpContext.Current.Session["SelectedMakes"];
     }

ViewState是页面的属性,它贯穿ASP.NET WebForms页面生命周期。将WebMethods与AJAX结合使用会跳过整个页面生命周期,并完全跳过ViewState

因此,您将无法按您的外观使用ViewState。为了使用AJAX并仍然能够访问所有WebForms,如ViewState和control属性,您需要使用UpdatePanel


您需要找到替代方法—例如,您可以将内容放入隐藏字段,而不是ViewState,然后使用javascript读取和填充这些隐藏字段。如果您这样做,您可以从javascript和ASP.NET世界读取和写入这些字段。

正如Joe Enos指出的,
ViewState
是页面实例的一部分,但您可以使用
会话
缓存,如下所示:

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

    }
[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;
}

注意:这还允许您将页面的一部分与ASP.NET AJAX页面方法一起使用,以便只向服务器获取或存储一些值,同时还允许您的页面回发通过
会话
访问数据。

这里有一些有趣的想法。“隐藏字段,我以前听说过,但从未在实践中使用过。@AlexanderRyanBaggett如果你离开WebForms的世界,你会看到隐藏字段的使用频率会更高。它们非常适合MVC,例如,您需要发布包含ID的完整模型,但不希望ID像输入字段中的其他属性一样可见。请记住,它们根本不是秘密的——不是加密的,甚至不是编码的,用户可以很容易地操纵它们——不同于ViewState,它总是经过验证以帮助防止简单的篡改,并且通常是服务器加密的。谢谢您的建议。我在电子商务公司工作的兄弟说,他们在他工作的地方使用了隐藏字段。这并不像我想象的那样有效。它仍然告诉我“非静态字段方法或属性'System.web.ui.Session.get'需要对象引用”我的非工作代码现在看起来如下:[WebMethod(EnableSession=true)][ScriptMethod]公共静态字符串populateModels(string[]MakeId){Session[“SelectedMakes”]=makeIds;}@AlexanderRyanBaggett-啊,是的,很抱歉忘记您从
HttpContext中抓取了
会话
。当前
,答案更新以反映这一点。是的,我在这里看到了。我可以给它分配一个字符串[],但我似乎无法从中提取字符串[]。它说的是在我试图检索它时强制转换它。基本上,我需要一种将它转换为字符串数组的方法,或者一种将单个元素作为字符串访问的方法。@AlexanderRyanBaggett-很抱歉我离开了一段时间,很高兴你解决了你的问题。祝你好运。:-)