title: How to increment a variable in ASP.NET (C#) between page views or button clicks
body: <p>I have the feeling that i'm missing something key here.</p>
  
  <p>I've tried following guides on <a href="http://msdn.microsoft.com/en-us/magazine/cc300437.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/magazine/cc300437.aspx</a>
  and on google, but i can't see what i haven't done.</p>
  
  <p>I have some very basic code which i have written just trying to get this to work:</p>
  
  <p>The Default.aspx code:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableSessionState="True" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Demo Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;asp:Label ID="myLabel" runat="server" Text="foo"&gt;&lt;/asp:Label&gt;
          &lt;asp:LinkButton ID="lnkClickButton" runat="server" OnClick="lnkClickButton_Click" CommandName="Clicky"&gt;Click Me&lt;/asp:LinkButton&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The Default.aspx.cs code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  public partial class _Default : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          Session["clickcount"] = 0;
          Cache["clickscount"] = 0;
      }
      protected void lnkClickButton_Click(object sender, EventArgs e)
      {
          Session["clickcount"] = (int)Session["clickcount"] + 1;
          Cache["clickscount"] = (int)Cache["clickscount"] + 1;
  
          Label myLabel = ((Label)this.FindControl("myLabel"));
          if (myLabel != null)
          {
              myLabel.Text = "Session: " + Session["clickcount"] + "; Cache: " + Cache["clickscount"] + ";";
          }
      }
  }
  </code></pre>
  
  <p>I've tried using both the session object and the cache object to increment the values, but to no avail. I just get 1 every time.</p>
  
  <p>N.B. this is my first asp.net project, also i'm fairly new to c#.</p>