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