title: Update counter when user clicks an asp.net web app imagebutton
body: <p>I have an ASP.NET web forms application where the user opens up an image on an ImageButton control. I set a global int variable "counter" to zero outside before defining methods. Every time the user clicks on the ImageButton control the "counter" is supposed to increase by one. The OnClick method associated with the ImageButton is firing, but I think the "counter" is being reset after each click. I know this because only the if branch in Image_Click is being executed. How can I make sure that the updated value of "counter" is remembered for each click?</p>
  
  <p>Here is .aspx code for ImageButton:</p>
  
  <pre><code>&lt;asp:ImageButton ID="pic" runat="server" OnClick="Image_Click" /&gt;
  </code></pre>
  
  <p>Here is c# code for Image_Click:</p>
  
  <pre><code>public int numClick++;
  
  protected void Image_Click(object sender, ImageClickEventArgs e)
  {
      numClick++;
  
      if (numClick % 2 == 1)
      {
          pos1x = e.X;
          pos1y = e.Y;
          labelarea.Text = " " + pos1x;
  
      }
      else if (numClick % 2 == 0)
      {
          pos2x = e.X;
          pos2y = e.Y;
          distx = Math.Abs(pos2x - pos1x);
          disty = Math.Abs(pos2y - pos1y);
          redistx = (int)(Math.Ceiling((float)(distx / (zoom * Math.Floor(dpiX / 4.0)))));
          redisty = (int)(Math.Ceiling((float)(disty / (zoom * Math.Floor(dpiY / 4.0)))));
          if (mode == 1)
          {
              if (distx &gt;= disty)
              {
                  lengthlabel.Text = "Length: " + redistx;
                  total += redistx;
              }
              else
              {
                  lengthlabel.Text = "Length: " + redisty;
                  total += redisty;
              }
              labeltotal.Text = "Total: " + total;
          }
      }
  }
  </code></pre>