会话不记得设置值

发布于 2024-11-07 18:00:55 字数 1015 浏览 1 评论 0原文

我正在尝试使用 ASP.NET 中的会话状态在两个页面之间传递数据。需要传递的数据来自 GridView 的单元格。因此,在 SelectedIndexChanged 事件中,我将这样存储数据:

protected void GridViewEmployees_SelectedIndexChanged(object sender, EventArgs e)
{
        Session["Name"] =   
           this.GridViewEmployees.Rows[GridViewEmployees.SelectedIndex].DataItem;
}

然后,我想在另一个页面中使用会话的内容。代码:

protected void Page_Load(object sender, EventArgs e)
{
        string name = (string)(Session["Name"]);
        string[] names = name.Split(' ');
        this.EntityDataSourceNorthwind.Where = 
           "it.[FirstName] = \'" + names[0] + 
           "\' AND it.[lastName] = \'" + names[1] + "\'";
}

但我得到一个空引用异常。另外,我已经在 Page_Load 事件上设置了会话初始化,并且会话被存储在那里,一切正常。我的意思是:

protected void Page_Load(object sender, EventArgs e)
{
        if (Session["Name"] == null)
        {
            Session["Name"] = "Andrew Fuller";
        }
}

这是在发送信息的页面中。

如果您需要更多来源或信息,只需写下来。将尽快提供。

提前致谢!

I am trying to pass data between two pages using Session State in ASP.NET. The data that needs to be passed is from a GridView's Cell. So, on SelectedIndexChanged event I'm storing the data like that:

protected void GridViewEmployees_SelectedIndexChanged(object sender, EventArgs e)
{
        Session["Name"] =   
           this.GridViewEmployees.Rows[GridViewEmployees.SelectedIndex].DataItem;
}

And then, I want to use the Session's contents in another page. The code:

protected void Page_Load(object sender, EventArgs e)
{
        string name = (string)(Session["Name"]);
        string[] names = name.Split(' ');
        this.EntityDataSourceNorthwind.Where = 
           "it.[FirstName] = \'" + names[0] + 
           "\' AND it.[lastName] = \'" + names[1] + "\'";
}

But I get a null reference exception. Also, I have set the Session initialization on the Page_Load event and there the Session is stored and everything works just fine. I mean:

protected void Page_Load(object sender, EventArgs e)
{
        if (Session["Name"] == null)
        {
            Session["Name"] = "Andrew Fuller";
        }
}

This is in the page which sends the information.

If you need more source or info, just write it down. It will be provided ASAP.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗心遗梦遗幸福 2024-11-14 18:00:55

这其实不是你的Session的问题。行的 DataItem 仅在 RowDataBound 事件期间可用。在 GridViewEmployees_SelectedIndexChanged 方法中设置断点,然后检查立即窗口中 DataItem 的值。它将为空。

This is actually not a problem with your Session. A row's DataItem is only available during the RowDataBound event. Set a break point in your GridViewEmployees_SelectedIndexChanged method, and check the value of the DataItem in your immediate window. It will be null.

杯别 2024-11-14 18:00:55

两个最可能的罪魁祸首是:会话状态被禁用 (EnableSessionState="false"),或者 Cookie 被禁用并且您没有使用 无 Cookie 会话

The two most likely culprits are either - session state is disabled (EnableSessionState="false"), or cookies are disabled and you're not using cookieless sessions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文