大家。我一直在努力按照教科书中的说法,但无济于事。现在,我的问题是我正在尝试创建一个页面计数器来跟踪每个页面的访问次数,然后在新页面上显示每个值。
这是所有页面的C#计数器代码:
int sessionCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["CountMain"] == null)
sessionCount = 0;
else
sessionCount = Convert.ToInt32(Session["CountMain"]);
sessionCount++;
}
protected void Page_PreRender(object sender, EventArgs e)
{
Session["CountMain"] = sessionCount;
}
预渲染是我在研究互联网和教科书后自己添加的东西。没有运气。
这是Counter Page C#代码:
public partial class Counter : System.Web.UI.Page
{
int sessionCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
string sessionMain = Session["CountMain"].ToString();
string sessionComment = Session["CountComment"].ToString();
string sessionCompleted = Session["CountCompleted"].ToString();
string sessionCurrent = Session["CountCurrent"].ToString();
string sessionAbout = Session["CountAbout"].ToString();
string sessionContact = Session["CountContact"].ToString();
string sessionCounter = Session["CountCounter"].ToString();
if (Session["CountCounter"] == null)
sessionCount = 0;
else
sessionCount = Convert.ToInt32(Session["CountCounter"]);
sessionCount++;
lblAboutCount.Text = sessionAbout;
lblCommentCount.Text = sessionComment;
lblCompletedCount.Text = sessionCompleted;
lblContactCount.Text = sessionContact;
lblCounterCount.Text = sessionCounter;
lblCurrentCount.Text = sessionCurrent;
lblMainCount.Text = sessionMain;
}
当我尝试运行它时,我得到一个“NullReferenceException未被用户代码处理,对象引用未设置为对象的实例。”错误。
提前致谢。
编辑#1
好的,感谢Hexxangonal,柜台现在正在运作。但是,我的Counter Page现在正在自我计算2次。 (每次加载时递增2)
public partial class Counter : System.Web.UI.Page
{
int sessionCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["CountCounter"] == null)
{
sessionCount = 0;
Session["CountCounter"] = sessionCount;
}
else
{
sessionCount = Convert.ToInt32(Session["CountCounter"]);
sessionCount++;
Session["CountCounter"] = sessionCount;
}
Session["CountCounter"] = sessionCount;
lblAboutCount.Text = Convert.ToString(Session["CountAbout"]);
lblCommentCount.Text = Convert.ToString(Session["CountComment"]);
lblCompletedCount.Text = Convert.ToString(Session["CountCompleted"]);
lblContactCount.Text = Convert.ToString(Session["CountContact"]);
lblCounterCount.Text = Convert.ToString(Session["CountCounter"]);
lblCurrentCount.Text = Convert.ToString(Session["CountCurrent"]);
lblMainCount.Text = Convert.ToString(Session["CountMain"]);
}
答案 0 :(得分:2)
NullReference异常可能来自您的Session["CountXXXXX"].ToString()
行(CountXXXXX是您的计数对象之一,如CountMain
),因为Session["CountXXXXX"]
不存在(它为空)。
您实际上可以将该页面简化为以下逻辑,并且您将绕过该问题,因为只会将null分配给字符串变量。
public partial class Counter : System.Web.UI.Page
{
int sessionCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["CountCounter"] == null)
sessionCount = 0;
else
sessionCount = Convert.ToInt32(Session["CountCounter"]);
sessionCount++;
// **NEW** Save the new count value
Session["CountCounter"] = sessionCount;
lblAboutCount.Text = Session["CountAbout"];
lblCommentCount.Text = Session["CountComment"];
lblCompletedCount.Text = Session["CountCompleted"];
lblContactCount.Text = Session["CountContact"];
lblCounterCount.Text = sessionCounter;
lblCurrentCount.Text = Session["CountCurrent"];
lblMainCount.Text = Session["CountMain"];
}
还有一个问题是你将整数和字符串与sessionCount
变量混合在一起,成员变量和局部变量(分别)。我已经清理过了。
答案 1 :(得分:2)
问题在于这两行
if (Session["CountCounter"] == null)
sessionCount = 0;
else
sessionCount = Convert.ToInt32(Session["CountCounter"]);
sessionCount++;
您没有将这些值放回会话变量中 你应该这样做: -
if (Session["CountCounter"] == null)
{
sessionCount = 0;
Session["CountCounter"]=sessionCount;
}
else
{
sessionCount = Convert.ToInt32(Session["CountCounter"]);
sessionCount++;
Session["CountCounter"]=sessionCount;
}