Detect web page refresh


/ Published in: C#
Save to your folder(s)

Overwrite LoadViewSate and SaveViewState page methods.
SaveViewState is called on initial page load and that is the place to save your negated flag to Session.
This value should be compared in LoadViewState and if the values are the same it means that the page is refreshed.


Copy this code and paste it in your HTML
  1. private bool refreshState;
  2. private bool isRefresh;
  3.  
  4. protected override void LoadViewState(object savedState)
  5. {
  6. var AllStates = (object[]) savedState;
  7.  
  8. base.LoadViewState(AllStates[0]);
  9. refreshState = bool.Parse(AllStates[1].ToString());
  10. isRefresh = refreshState == (bool) Session["IsRefresh"];
  11. }
  12.  
  13. protected override object SaveViewState()
  14. {
  15. Session["IsRefresh"] = refreshState;
  16. var AllStates = new object[2];
  17.  
  18. AllStates[0] = base.SaveViewState();
  19. AllStates[1] = !refreshState;
  20. return AllStates;
  21. }
  22. public void DoWork()
  23. {
  24. if(!isRefresh)
  25. {
  26. // your work here
  27. }
  28. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.