title: Where do I put my object when using asp
body: <p>I'm relatively new to C# and VS, and currently having a play with ASP.NET, but I seem to be getting very muddled on where I should place my objects that I need to use within my webforms.  As a very basic example, how could I add a counter button?  With the code below, when you click the button nothing changes and the textbox just shows The count is 2'.  I think this is because the page is reloading each time and therefore the Counter object gets 're' instantiated.  So how do I avoid that?</p>
  
  <p>Default.aspx
  ...</p>
  
  <pre><code>&lt;asp:Button ID="bttnCounter" runat="server" Text="Click Me" OnClick="ButtonClick"/&gt;
  </code></pre>
  
  <p>...</p>
  
  <p>Default.aspx.cs</p>
  
  <pre><code>public partial class _Default : Page
  {
  
      Counter counter = new Counter();
  
      protected void Page_Load(object sender, EventArgs e)
      {
          bttnCounter.Click += new EventHandler(this.ButtonClick);
      }
  
      public void ButtonClick(Object sender, EventArgs e)
      {
          counter.CountUp();
          output.Text = "The count is " + counter.CurrentCount;
      }
  }
  </code></pre>
  
  <p>Counter.cs</p>
  
  <pre><code>public class Counter
  {
      public int CurrentCount { get; private set; }
  
      public Counter()
      {
          CurrentCount = 0;
      }
  
  
      public void CountUp()
      {
          CurrentCount++;
      }
  }
  </code></pre>
  
  <p>I may have just completely mis understood this, but when I was using WinForms, I would include the object within the form code.</p>