【问题标题】:Page Counter not working ASP.NET C#页面计数器不工作 ASP.NET C#
【发布时间】:2013-01-18 18:35:27
【问题描述】:

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

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

prerender 是我在网上研究后自己添加的,在教科书中。没有运气。

这是计数器页面 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"]);
    }

【问题讨论】:

  • 哪一行给你带来了麻烦?你期待什么对象?它是从哪里传来的?
  • 啊,谢谢提醒。 lblAboutCount.Text = sessionAbout;是异常发生的地方。我正在尝试让 Session["Count???"] 传递到 Counter 页面上的值。

标签: c# asp.net web-applications web


【解决方案1】:

问题出在这两行

   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;
     }

【讨论】:

  • @RionMurphMurphy,但您是在计算单个用户还是每个用户。
  • 页面被访问了多少次,所以我猜每个用户。
  • @RionMurphMurphy 那么你的方法是错误的。因为会话对用户来说是个人的。这对每个用户来说并不常见。您应该使用应用程序变量而不是会话。
  • @RionMurphMurphy 这是一个很好的链接,这可能对你有用stackoverflow.com/questions/14034288/…
【解决方案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 变量与成员变量和局部变量(分别)混合。我已经清理过了。

【讨论】:

  • 啊,你也没有恢复那个 sessionCount 值!
  • 我要继续假设我不需要 PreRender 方法?
  • 不,没有必要在那里存储 Session 变量。特别是如果你的书没有提到它。如果你愿意,你可以使用它,但你最好将所有的逻辑都放在你的 Load 中。
  • 如果我可以提一下,在你的 lblAboutCount.Text = Session["CountAbout"];行,您可能希望将其更改为 lblAboutCount.Text = Convert.ToString(Session["CountAbout"]);
  • 啊,我假设它是一个字符串。 ToString 也应该在那里工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多