首页 > 解决方案 > 使用 javascript 的会话

问题描述

如何在不刷新页面的情况下管理会话?

描述:我在 First.aspx 页面上有一个模型弹出窗口,当单击 First.aspx 中的按钮时,使用 iframe 在弹出窗口中打开 Second.aspx 页面我想从Second.aspx(服务器端)获取值,并在弹出窗口关闭而不重新加载时session["Value"]显示在页面上First.aspxFirst.aspx 可能吗?

我试过这样 - 但是这个会话值在重新加载 First.aspx 后显示

.cs 代码 (First.aspx)

 Session["ComplaintValue"] = hfPresentComplaint.Value.Replace("'", "''");

Javascript 代码 ()

<script>
  function closepopupsession() {
    debugger;
    var a = ' <%=Session["ComplaintValue"]%>'
    if (a != null) {
      var hidnvalComplaint = document.getElementById("hidnvalueComplaint").value
      console.log(replaced);
    }
  }
</script>

标签: javascriptc#htmlasp.net

解决方案


有不同的技巧可以做到这一点,但在我的情况下,我使用 Ajax 每 1 秒调用一次单独的页面,我在那里有我的会话,并且索引页面中有一个空的 div,如下所示:

        function RefreshSessions()
        {
          var req = new XMLHttpRequest();
          req.onreadystatechange = function()
        {
             if(req.readyState == 4 && req.status == 200)
             {
              document.getElementById('EmptyDiv').innerHTML = req.responseText;
             }
        }
            req.open('GET', 'update_sessions.php', true);
            req.send();
        }
            setInterval(function(){RefreshSessions()}, 1000);

希望它会有所帮助。


推荐阅读