在一页上加载页面之前会话丢失,而在另一页上没有

时间:2014-01-14 10:47:38

标签: c# javascript asp.net webforms

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

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

此设置在我开发的第一个屏幕上工作正常,并且没有一次失败。问题是,在我以完全相同的方式设置的附加屏幕上,在父窗口的页面加载上检查会话值时,它只是设置为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;
}

父窗口检查会话值(这是会话在一个页面上返回null而在另一个页面上没有的位置:

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");
                }
}

我很感激这方面的任何帮助。

1 个答案:

答案 0 :(得分: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.Remove("CustTypeVal");

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

相关问题