1

大家。我一直试图完全按照教科书上的内容进行操作,但无济于事。现在,我的问题是我目前正在尝试创建一个页面计数器来跟踪每个页面被访问了多少次,然后在新页面上显示每个值。

这是所有页面都相同的 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;
    }

预渲染是我在网上研究后添加的,在教科书中。没运气。

这是计数器页面 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,计数器现在可以正常工作了。但是,我的计数器页面现在自计数了 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"]);
    }
4

2 回答 2

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带有成员变量和局部变量的变量(分别)混合在一起。我已经清理了这个。

于 2013-02-04T04:33:53.397 回答
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;
     }
于 2013-02-04T04:35:56.623 回答