views:

715

answers:

5

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.

A: 

why dont you do it with Javascript ?? this would be much easier.

xavoDev
because i actually do some stuff in the code behind. For example everytime the page is loaded, i increment the pageview index in the code behind.
zaladane
A: 

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

hermiod
A: 

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?

Russ Cam
A: 

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

xavoDev
A: 

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.

Magnus Bertilsson