Each time my webpage is loaded it runs a routine in the page_load event of the page behind which increments a viewcount by 1.
The problem I have is that the routine runs even if use refresh to reload the page.
How do I stop this routine from running if the page has been viewed by a particular user in their current session?
I thought I could use:
If Not Page.IsPostBack Then
(run the routine)
but it does not seem to work.
Do I need to use session state or cookies etc.?
You could record whether the user has visited the page within the session. You could just place a bool within the session under the page path. This way it’ll scope to the individual user and it’ll work for the duration of their session.
To record that the user has visited the page you could do the following:
and to get whether the user has visited the page you could do this:
Here’s how it would come together within your page load:
If you want to incorporate this technique on multiple pages I’d encapsulate this functionality into a helper class so you don’t keep repeating yourself.
Then it’d greatly simplify the load logic to the following:
(It would give you the added benefit of the logic being in a central location, which would be very handy if you needed to change it)