Persist Variable values

  • Thread starter Thread starter Looch
  • Start date Start date
L

Looch

I've run into what seems to be an issue with persisting state. I'm new
to ASP.NET but have been using C# for a while now. I'm coding a
button_click method in the .cs file for a web page and trying to track
the number of times the button has been clicked.

The issue is, everytime the button is clicked, the page seems to
reload and I'm losing the variable value that I'm using to keep track
of the number of times the button is clicked. i couldn't find a
propert setting

I'm guessing the easiest way would be to add the code into a script
section of the client side code, but what about using a browser other
than IE? Would the data still get refreshed when the button is
clicked? I get the feeling these are some of the fundemental questions
in web apps.

Thanks for any suggestions.
 
Do you want to track it specific to each user's session or the number of
times it is clicked 'application' -wide?

This has two very different implications. If you want to track per session,
in order to persist the value postback-to-postback, you can record it in the
Session object, a database using the caller's session key, xml file, etc...
and refresh (re-read) it each postback. You could perform this write
operation in the button's OnClick event.

If you want to track it for the application as a whole, you can store it in
the Application object but be aware that this will reset whenever the
application is restarted if you don't write it to persistent storage.

Some other options include cookies, writing the value to the ViewState or
hidden form fields but I'm an advocate of the other approach as it doesn't
depend on the user to keep cookies alive or store what you consider to be
important on the user's browser.
 
Back
Top