I've seen a number of question on how to get a Session variable into knockout, but none explaining how to set one from a Knockout vm. My setup:

我已经看到了一些关于如何将一个Session变量转换为knockout的问题,但没有一个解释如何从Knockout vm中设置一个。我的设置:

-ASPX page where I get a Session["GridSize"] variable, and return it as a global variable called currentGridSize

-ASPX页面,我得到一个Session [“GridSize”]变量,并将其作为一个名为currentGridSize的全局变量返回

-VM where I get that global variable, and set this.gridSize = globals.gridSize

-VM我得到那个全局变量,并设置this.gridSize = globals.gridSize

-Dropdown that changes this.gridSize

-Dropdown更改this.gridSize

What I need:

我需要的:

-Some way to set Session["GridSize"] = this.gridSize, either when it changes or when the page is left

- 一些设置Session [“GridSize”] = this.gridSize的方法,无论是更改还是页面保留时

I have tried:

我努力了:

-Using a webmethod function on my .aspx.vb and calling that (Session variables cannot be called from a Shared function, and webmethods must be shared)

- 在我的.aspx.vb上使用webmethod函数并调用它(无法从共享函数调用会话变量,并且必须共享webmethods)

-Calling <%Session["CurrentPageIndex"]= self.currentPageIndex();%> from the vm

从vm中调用<%Session [“CurrentPageIndex”] = self.currentPageIndex();%>

1 个解决方案

#1


1

You can access Session in an ASP.NET AJAX Page Method, by doing the following to your page method:

您可以通过对页面方法执行以下操作来访问ASP.NET AJAX页面方法中的Session:

<WebMethod(EnableSession := True)> _
Public Shared Sub StoreSessionValue(sessionValue As String)
    ' Set a value into Session
    HttpContext.Current.Session("TheSessionValue") = sessionValue
End Sub

<WebMethod(EnableSession := True)> _
Public Shared Function GetSessionValue(sessionValueName As String) As String
    ' Get a value from Session
    Return HttpContext.Current.Session(sessionValueName)
End Sub

Note: You must fully qualify the Session object as HttpContext.Current.Session.

注意:您必须将Session对象完全限定为HttpContext.Current.Session。

You can call this page method inside of your view model function, like this:

您可以在视图模型函数中调用此页面方法,如下所示:

$.ajax({
  type: "POST",
  url: "YourPage.aspx/GetSessionValue",
  data: "{'sessionValueName':'sessionValue'}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(data) {
    // Do something with data returned here

  }
});

更多相关文章

  1. 在页面加载时在shadowbox中加载视频
  2. js闭包与变量
  3. 有没有办法通过javascript检测关闭页面?
  4. JavaScript声明全局变量三种方式的异同
  5. JS在页面加载时候onload与匿名自调用函数的区别
  6. 父范围中的变量不会在匿名函数中被更改[重复]
  7. 传递变量以便以角度移动子弹
  8. 在变量名是字符串时声明变量?
  9. 如何将变量推送到web客户端以获取ajax?

随机推荐

  1. android 绿色开发环境
  2. [Android Pro] 完美Android Cursor使用例
  3. Android:通过Camera进行拍照
  4. Android知识点记录:七牛Android(安卓)SDK
  5. 在CentOS下搭建Android 开发环境
  6. Android——build.prop 解析
  7. 自定义ListView中的分割线
  8. Android——体系架构 +四层
  9. 疯狂android讲义学习总结---toggle与swit
  10. android 开发 解码gif图片,获取每帧bitmap