【问题标题】:Session lost before page load on one page but not on another在一个页面上的页面加载之前会话丢失,但在另一个页面上没有
【发布时间】:2014-01-14 10:47:38
【问题描述】:

我正在开发一个在许多页面上使用的具有类似功能的应用程序。我有一个屏幕,它打开一个模式窗口并允许用户进行搜索。当用户单击记录时,记录名称首先保存在会话中,然后要求父窗口关闭模式然后刷新。

然后在父页面加载时,我先检查一下搜索文本会话的值是什么,然后再使用它来搜索其他数据并填充屏幕。

此设置在我开发的第一个屏幕上运行良好,并且没有失败过一次。问题是,在我以完全相同的方式设置的另一个屏幕上,在父窗口的页面加载上检查会话值时,它只是设置为 NULL,我什至从工作中复制了相同的代码页面到这个,所以它应该是相同的。

子窗口JS:

  <script type="text/javascript">
        function sendval() {
            window.parent.onSave(); - Just switches off a checkdirty function.
            window.parent.location.reload(); - Reloads the parent so it can get the val on page load
        }
        </script>

子窗口按钮:

<asp:Button ID="Button1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "AddressCode") %>' OnClientClick="sendval();" OnClick="Button1_Click" />

子窗口按钮方法:

protected void Button1_Click(object sender, EventArgs e)
{
    string buttonText = ((Button)sender).Text;
    Session["CustTypeVal"] = buttonText;
}

父窗口检查会话值(这是会话在一个页面上返回为空但在另一页面上不返回的地方:

protected void Page_Load(object sender, EventArgs e)
            {
//get the session variable from the open page in order to do the search.
                if (Session["CustTypeVal"] != null)
                {
                    //The method you need to run after refresh
                    SearchInvAddr((string)Session["CustTypeVal"]);
                    //Remove the session after
                    Session.Remove("CustTypeVal");
                }
}

我将不胜感激。

【问题讨论】:

  • 还有更多代码可以分享吗?我在下面添加了我的回复,但更多代码将有助于更完整地回答您的问题。未正确获取 Session 值的页面代码会有所帮助。

标签: c# javascript asp.net webforms


【解决方案1】:

你的代码在这里

protected void Page_Load(object sender, EventArgs e)
            {
//get the session variable from the open page in order to do the search.
                if (Session["CustTypeVal"] != null)
                {
                    //The method you need to run after refresh
                    SearchInvAddr((string)Session["CustTypeVal"]);
                    //Remove the session after
                    Session.Remove("CustTypeVal");
                }
}

如果这是您的第一页,并且您在第二页上有完全相同的代码,则 Session 值将不再存在,因为

Session.Remove("CustTypeVal");

如果您正在使用 Session 值,则在您注销之前不必将其清除,因此没有真正需要删除它们,除非它可能被用于违反安全性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 2013-01-28
    • 1970-01-01
    相关资源
    最近更新 更多