views:

117

answers:

3

Hello,

I need to detect when a user refreshing the page in the Page_Init event to stop them from executing the same code twice. What's the way to do this in ASP.NET

thanks

+1  A: 

Yes, the Page.IsPostBack property:

Gets a value indicating whether the page is being loaded in response to a client postback, or if it is being loaded and accessed for the first time.

Andrew Hare
I believe he's talking about a page refresh (e.g., press F5), not a postback.
patmortech
Page.IsPostBack doesn't seem to work. It's always false. The post back isn't coming from a control, its if you refresh your browser
jumbojs
You probably shouldn't be doing anything non-repeatable on a GET request anyway - it shouldn't have any side effects, and should potentially be able to be cached - you should be using a POST of some kind to perform your non-repeatable actions, and then you would be able to check your IsPostBack correctly.
Zhaph - Ben Duguid
+2  A: 

Here's an article that talks about some different ways of dealing with this problem, though I'm not sure it matches 100% with your situation.

patmortech
+1 there are various solutions in the article that should in one way or another address most any issue in this problem class.
Rex M
A: 

You can set cookie with short expiration time after first request on your site. Then you can simply check it and enlarge the cookie expiration.

Jan Remunda