Difference between IsPostBack and upgrade in Asp.Net

I wrote some codes in the !IsPostBack block. This code runs when the page loads for the first time. This is normal. But the problem is that when I refresh the page by pressing f5 , this is done again, which I do not want to do. I searched a lot of articles and found the difference between PostBack and the update. I know about it. But my question is the difference between !IsPostBack and Refresh. Can we write code that runs only when the page loads for the first time, when we do not refresh the page. By the way, I wrote my block !IsPostBack inside the Page_Init() method, and I use C # for codebehind. Thanks.

+5
source share
2 answers

Refersh and IsPostback are somewhat unrelated:

  • Updating in a browser usually means "re-executing the last action that led to this page." It usually calls a GET request, but it can also call POST if the page was shown as the result of a postback. Side of the note: you can often find sites warning you not to refresh the page during irreversible operations, such as “charge my credit card”, as this can lead to duplicate messages.
  • IsPostBack simply states that the request arrives at the server as POST, not GET.

Combining this, you can get Refresh, which launches the if (IsPostBack) check branch. In most cases, the server receives a GET request and therefore executes the !IsPostBack branch.

If you really need to determine if the page has already been done, setting a cookie or writing information to Session would be a smart decision.

+10
source

Please change code by code as shown below.

  Boolean IsPageRefresh; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["postids"] = System.Guid.NewGuid().ToString(); Session["postid"] = ViewState["postids"].ToString(); } else { if (ViewState["postids"].ToString() != Session["postid"].ToString()) { IsPageRefresh = true; } Session["postid"] = System.Guid.NewGuid().ToString(); ViewState["postids"] = Session["postid"].ToString(); } } 
-1
source

Source: https://habr.com/ru/post/1210526/


All Articles