views:

228

answers:

2

I have some code in the load event of my page which should only run the first time the page is loaded, however, it runs everytime 'refresh' is clicked on the browser EVEN THOUGH i am checking for postbacks:

If not page.ispostback then
 ' Code...
End if
+2  A: 

Refresh isn't a postback (i.e. there's no POST happening, it's another GET), it's the browser asking for the page all over again. In this case, from the server point of view, it's a fresh request.

Nick Craver
How do I deal with that?
burntsugar
@burntsugar - What are you trying to only run once? Maybe there's another approach.
Nick Craver
Seems like he wants to prevent refreshs and/or treat them like PostBacks.
hunter
Now reading: Trap the Browser Refresh http://msdn.microsoft.com/en-us/library/ms379557%28VS.80%29.aspx#bedrockas_topic2
burntsugar
A: 

The IsPostBack property can only detect requests that were generated by a __doPostBack JavaScript function call. All other requests are treated as new requests since ASP.NET has no way of knowing how the request was generated.

Since the __doPostBack function populates hidden form fields, the ASP.NET runtime is able to detect that the request was generated by a control which posted back. All other requests will not have populated these hidden fields so the runtime does not consider them post-backs.

Andrew Hare