ak3nat0n
ak3nat0n

Reputation: 6298

Detecting page refresh without viewstate

Is there a way to detect page refresh without using viewstate in asp.net webforms ? I was wondering if it was at all even possible since asp.net mvc doesn't use viewstate for example.

I am trying to avoid using viewstate alltogether to keep my page size small.

Upvotes: 2

Views: 1959

Answers (4)

Magnus Bertilsson
Magnus Bertilsson

Reputation: 615

The http protocal is stateless and after a response is send to the one how requestet it removes all trace of him except his session. The way webforms work is that it serialize your page state and send it to the client and then back to the server when you make a postback(A form post request). So you can save information about your client in his session about the page he been on before and then check in the session if he request the same page again.

Upvotes: 0

xavoDev
xavoDev

Reputation: 51

IsPostBack is not going to work because its a F5 or Pager refresh case not the post back from any event.

Upvotes: 1

Russ Cam
Russ Cam

Reputation: 125538

You can detect a page refresh on the server-side using IsPostBack. In fact, any code you put in Page_Load in your code-behind will run each time the page is refreshed. Or have I misunderstood your question?

Upvotes: 0

hermiod
hermiod

Reputation: 1168

Why can't you just use IsPostback in the code behind with ViewState disabled in the page? This way you can do whatever you need to in the code behind and not worry about ViewState clogging up your page size.

You can disable ViewState in the page by adding the enableViewState flag to the @Page directive.

The only other thing I can think of is to have a piece of javascript code which catches the key press of F5, but obviously this will only capture an F5 refresh and not a Refresh-button refresh.

This site shows how to capture key presses with Javascript:
http://www.go4expert.com/forums/showthread.php?t=2733

Upvotes: 0

Related Questions