Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between !IsPostBack and refresh in Asp.Net

Tags:

c#

asp.net

I have written some codes in !IsPostBack block. This code is getting executed when the page loads for the first time. That is fine. But the problem is, when I refresh the page by hitting the f5 key this executes again which I don't want to do. I have searched many articles and found difference between PostBack and refresh. I know about this. But my question is difference between !IsPostBack and Refresh. Can we write some code which executes only when the page loads for the 1st time not when we refresh the page. By the way I have written my !IsPostBack block inside Page_Init() method and I am using c# for codebehind. Thanks.

like image 680
John Avatar asked Jan 07 '15 07:01

John


1 Answers

Refersh and IsPostback are somewhat unrelated:

  • Refresh in browser generally mean "re-run last action that resulted in this page". Usually it causes GET request, but it as well can cause POST if page was shown as result of postback. Side note: you often can find sites warning you not to refresh page during non-reversible operations like "charge my credit card" as it may trigger duplicate post.
  • IsPostBack simply states that request come to server as POST, not GET.

Combining that you can get Refresh that triggers either branch of if (IsPostBack) check. In most cases so server will receive GET request and hence execute !IsPostBack branch.

If you really need to detect if page was rendered once already - setting cookie or writing information into Session would be reasonable solution.

like image 77
Alexei Levenkov Avatar answered Oct 02 '22 04:10

Alexei Levenkov