会话不记得设置值
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这其实不是你的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.
两个最可能的罪魁祸首是:会话状态被禁用 (
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.