tags:

views:

252

answers:

7

I am using VS 2005, C# 2, ASP.Net 2.0

I am unable to find out how to track that user pressed F5/Ctrl+F5/ Open a new Window(Ctrl + N) in ASP.Net. I know that there is a Page.IsPostBack property, which tells that a page is loaded in response to an action taken by user.

I am just curious to know, that why isn't there a property as IsRefresh or Page.IsRefresh in ASP.Net, which will return true, whenever user takes any of the above actions.

Is there a way to know this?

Actually my problem is that i have a DLL using which all of my aspx pages are inherited, I have to insert some values in a table whenever the page is opened for the first time that's it, if user just opens the page or do not take any action, an entry should be inserted into the database, but as far as I have tried, I controlled it anyhow using the Page.IsPostBack property, but I got stuck in the refresh case, as it is inserting records unconditionally.

A: 

Perhaps you want the Session_Start method in the Global.asax file, which will be triggered once at the start of each user session?

In your Global.asax file, add or edit the method:

void Session_Start(object sender, EventArgs e) 
{
}
Winston Smith
A: 

why isn't there a property as IsRefresh or Page.IsRefresh in ASP.Net

Because ASP.NET cannot possibly know. The browser does not send any information that could allow it to determine whether the page is being requested due to a refresh or normal load. You will need to reconsider your requirements: what is the actual purpose of the database logging?

bobince
actually it is for saving the page opened by the user once he logged into the website/application with all the QueryString parameters and the time he opened the page in a table. what i do not want to make a duplicate entry in the table, if he refreshes the page unconditionally or unknowingly. kindly please help me in this.
Everest
A: 

Session_Start method in Global.asax file is fired every time when a browser session is started. You can use this method to count number of unique users on your website.

Session_End method in Global.asax is fired when a session ends (explicitly or timedout). So you can decrement the count here.

Hope the above to example uses of these methods helps you understand how you can use them.

Ismail
A: 

Because of the stateless nature of HTTP protocol there is no way to tell apart the initial load from the refresh

mfeingold
Then it means there is no Server Variable or Request.ServerVariables data available in the property which will tells that the page is refreshed.
Everest
A: 

As has already been said. This isn't possible. A request issued due to a refresh is no different to a request issued the first time the page is loaded.

It sounds to me like you are trying to track page views somehow. This is certainly possible though it will require some work on your part. Your best bet is probably to log the URL of the page. You may also want to include the query string in order to differentiate between page loads for different pieces of data (if this happens in your application). You will also want to log the ID of the current user, and the ID of their session.

You can then make sure that you don't insert two page views for the same user for the same page in the same session, effectively filtering out any reloads of a page.

You do need to be aware that this isn't the same as detecting a refresh, what you are detecting is two page views in the same session, this could be a refresh, or it could be use of the back button, or just reloading from the address bar.

Jack Ryan
A: 

My suggestion would be to create a cookie on very first load, then on Page_Load check to see if the cookie exists. If it does, don't insert the record. You can use Session_End to destroy or create the cookie as someone suggested if that works with your application's architecture.

Chris Laythorpe
A: 

Similar to using a function in Global.asax (as others have suggested) you could use a session variable "flag". When the page first loads set a session variable and then just check against it in your page load function:

if (Session("visited") != "true"
   //page has not been visited, log visit to DB

Just make sure you set the session flag sometime after the above check during the page load.

It won't be exact (sessions can timeout while a page is active, users can completely leave the site and come back in the same browser and the session stays alive, etc) but for your tracking it is much better than counting every page hit in the DB.

ktharsis

related questions