How to count the number of Button clicks?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pbala
    New Member
    • Dec 2008
    • 37

    How to count the number of Button clicks?

    if I declared
    Code:
     protected void Button1_Click(object sender, EventArgs e)
            {
     count++;
    Last edited by PRR; May 25 '09, 06:26 AM. Reason: Please do not double post.
  • pbala
    New Member
    • Dec 2008
    • 37

    #2
    How to count the number of Button clicks?

    if I declared
    Code:
     protected void Button1_Click(object sender, EventArgs e)
    {
    int count=0;
    count++;
    int noofcount=count;
    }
    it will results only 1. For Every click the count is changed to 0....
    Last edited by PRR; May 25 '09, 06:19 AM. Reason: Added code tags. Please post code in [code] [/code] tags.

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      Declare your count variable outside the Button1_Click event handler... As of now the count variable is declared and initialized on click event..

      Comment

      • balame2004
        New Member
        • Mar 2008
        • 142

        #4
        Better to declare count variable to be static inside the event method..

        Code:
        protected void Button1_Click(object sender, EventArgs e) 
        { 
        static int count=0; 
        count++; 
        int noofcount=count; 
        }
        Last edited by Frinavale; May 25 '09, 01:35 PM. Reason: Added code tags. Please post code in [code] [/code] tags.

        Comment

        • PRR
          Recognized Expert Contributor
          • Dec 2007
          • 750

          #5
          Originally posted by balame2004
          Better to declare count variable to be static inside the event method..

          protected void Button1_Click(o bject sender, EventArgs e)
          {
          static int count=0;
          count++;
          int noofcount=count ;
          }
          Static variable are used in context of class ... They cant be used inside functions...
          You need to declare static variables in form class or default Program class.. They are initialized before execution of the static constructor or other constructor ...

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Here is an example of what DeepBlue is talking about:

            C#:
            Code:
            static int count = 0; 
            // the count variable's scope is for the class (the page)
            // it can be used through out the class's (page's) code
            // it is shared between all instances of this class
            // static variables must be declared outside of any functions
            // since they have a class scope
            // they are shared between all instances of the class
            
            protected void Button1_Click(object sender, EventArgs e)
            {
            //if the variable had been declared here it would have a scope 
            //for the Button1 Click function only.
              count++;
            }
            VB:
            Code:
            Shared count As Integer
            
            ' the count variable's scope is for the class (the page)
            ' it can be used through out the class's (page's) code 
            ' it is shared between all instances of this class.
            ' shared variables must be declared outside of any functions
            ' since they have a class scope
            ' they are shared between all instances of the class
            
            Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            'if the variable had been declared here it would have a scope 
            'for the Button1 Click function only.
              count+=1
            End Sub

            Another thing to keep in mind is the ASP.NET page life cycle:
            • A request for the page is made
            • Your page is compiled
            • Your Controls and Objects are loaded
            • The page Load event is executed
            • Any control events are are executed
            • The page is rendered and sent to the browser
            • All Objects are destroyed


            This means that if you don't declare your variable as Static/Shared,your count variable is going to be destroyed when the Page Object is destroyed. You have to store this value somewhere so that you can persist (remember) the count value between page requests.

            You can use Session, ViewState, Cookies, or HiddenFields to do this.

            -Frinny

            Comment

            • balame2004
              New Member
              • Mar 2008
              • 142

              #7
              Or you can declare the variable as static. Now the value of the variable won't change during page round trips.

              Code:
              static int count = 0;  
                
              protected void Button1_Click(object sender, EventArgs e) 
              { 
              //if the variable had been declared here it would have a scope  
              //for the Button1 Click function only. 
                count++; 
                int noofcount=count; 
              }
              Last edited by Frinavale; May 26 '09, 01:19 PM. Reason: Added code tags. Please post code in [code][/code] tags.

              Comment

              • Bassem
                Contributor
                • Dec 2008
                • 344

                #8
                Originally posted by balame2004
                Or you can declare the variable as static. Now the value of the variable won't change during page round trips.
                What do you mean with this part?
                I didn't understand the difference!

                Thanks,
                Bassem

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Originally posted by Bassem
                  What do you mean with this part?
                  I didn't understand the difference!

                  Thanks,
                  Bassem
                  When you declare a variable as Static (C#) or Shared (VB.NET) this variable is used by all instances of the class.

                  In this case, since the Page is a class, the variable will be shared between all instances of that page.

                  What does this mean?

                  Well, a user comes to the page, an instance of your asp Page class is created (by ASP.NET). When the user clicks the button the static/shared variable is updated by that instance of your aspx Page Class.

                  Now the same user, clicks the button again...an instance of your aspx Page class is created and the same static/shared variable is updated.

                  Even if another user comes to the page...ASP.NET creates an instance of your aspx Page class which uses the same static/shared variable.

                  Make sense now?

                  Comment

                  • Bassem
                    Contributor
                    • Dec 2008
                    • 344

                    #10
                    Yes, thanks a lot Frinny. It was a good idea to be used.

                    Many thanks,
                    Bassem

                    Comment

                    • dnanetwork
                      New Member
                      • Nov 2006
                      • 12

                      #11
                      user static variable or viewstate...

                      Comment

                      • tangerine
                        New Member
                        • Jul 2014
                        • 1

                        #12
                        thanks frinny for this!!! cleared my concept

                        Comment

                        • ZeeshanAli
                          New Member
                          • Aug 2014
                          • 3

                          #13
                          Why do you want a Server Click? If its required so you may use View States or Session State however Static variable can not be use in a public method.
                          Instead of Server Click you can use Client Click in you web page.

                          For example.
                          Code:
                          var count = 0;
                          
                          function inc_num(){
                          count++;
                          // do something
                          }
                          
                          <button type="button" onclick="inc_num()" value="Increment" />
                          Last edited by Rabbit; Aug 28 '14, 04:11 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

                          Comment

                          Working...