views:

198

answers:

3

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.?

+2  A: 

The If Not Page.IsPostback will only help if the user clicks a button on that page. If the user just refreshes the page (like using F5) it won't work. The problem is

  • do you want to add to the counter if the user comes back to the page after he's been to another page on your site (i.e. homepage -> productpage -> homepage) or not.

You could use a Dictionary and store it in the user's Session. The Dictionary would be a Dictionary of type Dictionary<string, int>. When the user visits the page, you retrieve the dictionary and see if there is already an entry for the current page + querystring (the string Key).

if not, add it. Then depending on whether you want to increment the count for that page if the user revisits the page after first going to another page increment the counter (or not).

you can check if the user's came from another url by using: Request.UrlReferrer

Colin
+1  A: 

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:

HttpContext.Current.Session[pagePath] = true;

and to get whether the user has visited the page you could do this:

bool hasUserVisitedPage = (bool)HttpContext.Current.Session[pagePath];

Here's how it would come together within your page load:

protected void Page_Load(object sender, EventArgs e)
{
    //set the default for whether the user visited the page
    bool hasUserVisitedPage = false;
    //get the path of the page
    string pagePath = HttpContext.Current.Request.Url.LocalPath;

    //find out if the user visited the page by looking in the session
    try { hasUserVisitedPage = (bool)HttpContext.Current.Session[pagePath]; }
    //we don't care if the value wasn't present (and therefore didn't cast)
    catch {}

    //if the user hasn't visited the page before
    if (!hasUserVisitedPage )
    {
        //record that the page has now been visited
        HttpContext.Current.Session[pagePath] = true;

        //put the rest of your load logic here...
    }
}

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.

public static class PageHelper
{
    public static bool hasPageBeenViewed()
    {
        //set the default for whether the user visited the page
        bool hasUserVisitedPage = false;
        //get the path of the page
        string pagePath = HttpContext.Current.Request.Url.LocalPath;

        //find out if the user visited the page by looking in the session
        try { hasUserVisitedPage = (bool)HttpContext.Current.Session[pagePath]; }
        //we don't care if the value wasn't present (and therefore didn't cast)
        catch {}

        //if the user hasn't visited the page before
        if (!hasUserVisitedPage )
        {
            //record that the page has now been visited
            HttpContext.Current.Session[pagePath] = true;
        }

        return hasUserVisitedPage;
    }
}

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)

protected void Page_Load(object sender, EventArgs e)
{
    //if the user hasn't visited the page before
    if (!PageHelper.hasPageBeenViewed())
    {            
        //put the rest of your load logic here...
    }
}
DoctaJonez
+1  A: 

Refer link below

. Detect Browser refresh in ASP.NET to stop events getting fired again

amiT jaiN