[Solved] Can’t increment value of a Page variable in Asp.net on button click


The reason is that c is an instance variable on that class. And each post-back to the server creates a new instance of the class. So c is being initialized to 0 on each post-back. If you want c to persist outside of a single instance of that class, you need to store it somewhere. Session, application, a database, etc. (Specifically where depends on under what circumstances the value of c should persist. I’m guessing this is just test code to get a feel for the functionality, so you might try all of the above and see how they differ.)

For a lot more information on this, you’ll want to read up on the ASP.NET Page Life Cycle.

Essentially, “global” doesn’t describe c very accurately as a variable. It’s scope is very limited to only that class, and more specifically only to any one instance of that class.

solved Can’t increment value of a Page variable in Asp.net on button click