Tuesday, April 29, 2008

How to prevent Post back event when page refresh

One annoying behavior of ASP.NET pages is a replay of postback events when user hit refresh (F5) in browser.
This behavior is easily reproducible:
Place ASP button on Web Form
1. Add some code to Button_Click event
2. View page in browser
3. Check that your code being executed when you hit button
4. Refresh page and observe that your code being executed once again

The are a couple of solutions to get around this. But the easiest way is like this,

At the end of eventhandler, ie. button click, itemcommand, insert a line of code.

Response.Redirect("thispage.aspx ");

Which means, just redirect to the same page.

Also, this can be used to disable Back button

Eric

No comments: