Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
anyone tell me how to count integer value using various button click.i am having five button in my project when i click the first button count one the integer value then click second button again i need count value 2 but coming count value one tell me the solution asap

C#
protected void click_first(object sender, EventArgs e)
   {
       div2.Visible = true;
       div1.Visible = false;
       div3.Visible = false;
       div4.Visible = false;
       div5.Visible = false;
       count = count + 1;
   }
   protected void click_second(object sender, EventArgs e)
   {
       div3.Visible = true;
       div1.Visible = false;
       div2.Visible = false;
       div4.Visible = false;
       div5.Visible = false;
       count = count + 1;

   }
   protected void click_third(object sender, EventArgs e)
   {

       div1.Visible = false;
       div2.Visible = false;
       div3.Visible = false;
       div4.Visible = true;
       div5.Visible = false;
       count = count + 1;
   }
   protected void click_forth(object sender, EventArgs e)
   {
       div1.Visible = false;
       div2.Visible = false;
       div3.Visible = false;
       div4.Visible = false;
       div5.Visible = true;
       count = count + 1;
   }
Posted
Comments
demouser743 19-Nov-11 7:16am    
Declare count as Static int so that the value will be constant

Is this really ASP.NET? Because if so, values are not persisted across pages. And every time you press a button, you get a new page load, so you get the original (zero) value of count back.

Perhaps you need to save the value in a Session variable?
 
Share this answer
 
Comments
RaisKazi 19-Nov-11 7:27am    
5ed. Session will work. Alternative is "ViewState", as its the same page and "ViewState" gets maintanied event after PostBacks of the Events.
Use "ViewState".

Have a look at below link.
http://msdn.microsoft.com/en-us/library/bb386448.aspx
 
Share this answer
 
 
Share this answer
 
Comments
RaisKazi 19-Nov-11 7:34am    
5ed. For "Session" information.
make that count variable Static globally

static int count;


Hope it will help u
 
Share this answer
 
Comments
RaisKazi 19-Nov-11 7:39am    
I wouldn't vote, but static is not the solution for this as Question is for Asp.Net(Web). Static variable is kind of a common/shared variable across users. And hence its value will keep over-written by all the users. Eventually you will get count of all users click and not user specific click count.
Use a session or Application variable and store the clicks in that variable for a particular session.

If you want it at any time, try to store the clicks in the database
 
Share this answer
 
If you declare the count variable as Static then there will be no issues

Initially declare as follows

C#
static int count=0;
 
Share this answer
 
Comments
RaisKazi 19-Nov-11 7:40am    
I wouldn't vote, but static is not the solution for this as Question is for Asp.Net(Web). Static variable is kind of a common/shared variable across users. And hence its value will keep over-written by all the users. Eventually you will get count of all users click and not user specific click count.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900