【发布时间】:2012-06-11 11:24:41
【问题描述】:
所以在 Default.master.cs PageLoad 我有以下内容:
contObj = Session["Contributor"] == null ? null : (Contributor)Session["Contributor"];
if (contObj == null)
{
Session["Contributor"] = new Contributor
{
ID = id,
Name = name,
Email = email
};
}
在 Default.aspx.cs 中,我试图通过在按钮单击事件上执行此操作来获取对象:\
contObj = (Contributor)Session["Contributor"];
但是这会导致以下异常! 对象引用未设置为对象的实例。
我看不出我做错了什么。 能详细点吗?
谢谢!
【问题讨论】:
-
如果你在 if(Page.IsPostBack) 中做的话,你能确定会话变量在集合中吗
-
这包含在 MasterPage 的 PageLoad 内的
if (!Page.IsPostBack)中。 -
所以您在母版页中获得会话?
-
是的,我正在母版页中获取会话 - 我是否应该将代码块从 master 移动到 default.aspx? - 我应该提到它适用于 Firefox,但不适用于 IE - 这是一个非常奇怪的问题。
-
非常奇怪的行为检查页面标签中是否有 EnableSessionState="true" ?