Link to home
Start Free TrialLog in
Avatar of KevinJones
KevinJones

asked on

detecting browser refresh

Can anyone tell me how I can detect a browser refresh in IE?

I have a .NET webapp and want to update the current selection on the form when the user hits the refresh button, refresh menu item or F5.

It doesn't appear possible to detect a refresh on the server by looking at the event and the form data.  From what I am seeing, a refresh resends the previous event along with its event data.  So, there isn't anything unique that I can look for.

I am not having any luck finding a solution on the client either but I am hoping that someone may know a trick that I don't.

One thing different that I am seeing on the refresh is the confirmation message, "This page cannot be refreshed without resending the information . . . <Retry to resend> or <Cancel>".  From what I can glean from newsgroups, I get that message because I have hidden variables on the form.  I don't know if that factors into the mix or not.
Avatar of Jeex
Jeex
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi,

Add the following meta tag to the head section of your page:

<head>
  ...
  <meta http-equiv="Pragma" content="no-cache" />
  ...
</head>

That will force the browser to re-fetch the page on a refresh rather than load it from cache.

Cheers,

-- Jeex
Avatar of KevinJones
KevinJones

ASKER

I stuck that in my html but I don't see any difference.

On the server side, I am seeing that the browser refresh is resulting in the previous event being re-ran.

For example, if I click on a button in my form with the id"btnMyButton", then on the server, I see the click event for "BtnMyButton" called.  If I then click the refresh button on the browser, the click event for "BtnMyButton" gets called on the server again.  Instead, I want a way to know on the server that the browser's refresh button (or F5 or view menu->refresh) was clicked.
You could use the OnLoad event and attach this to the Body tag.  Then have whatever functionality you need tied to the refresh tied to the OnLoad event.  The only downfall to this is the OnLoad event gets called every time the page is loaded, so it would get called on the inital loading of the page, and also with every subsequent refresh.  Here's an example:

<body onLoad()="onRefresh()">

where onRefresh() would be the function doing whatever your page needs to do when the user Refreshes the page.

Jason
ASKER CERTIFIED SOLUTION
Avatar of Sapphireblue
Sapphireblue
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
er. foo, not foo().
Ok, this wasn't the answer but it jogged my brain enough to help me come up with a solution.

The solution:

Keep up with a refresh iteration using a hidden field on the form and a session state variable on the server.  During the postback, increment the iteration and store it in the form's hidden var and in the server's session var.

Since the browser refresh resends the previous request, the form's hidden var will be 1 less than the server's session var.  That is how I am catching a refresh on the browser.

One note, my app only has one web page.  Navigating between pages may complicate this method a bit.