Pressing the button again raises an event when the page is refreshed with F5

I found that if you hit F5 or refresh the browser window, the last event will fire again. Ex. I clicked on the button when the button event is executing normally, but if I press F5 from the browser window the same event is fired again.

Can anyone think about it?

Thank you for sharing your valuable time.

+2


source to share


3 answers


According to NinenthSense, how the browser reacts when the user refreshes the page.

If you still want to restrict you can go for some javascript like below



//to avaoid pressing F5 key

document.onkeydown = function()
 {
          if(event.keyCode==116) {
          event.keyCode=0;
          event.returnValue = false;
          }
}

//to avoid refresh, using context menu of the browser

document.oncontextmenu = function() {event.returnValue = false;}

      

+5


source


If you want to completely clear the page after the postback occurs so that it doesn't fire again, you can execute Response.Redirect on the same page.

Response.Redirect(Request.Url.AbsoluteUri);

      

This basically takes all your request and sends the browser back, clearing any messages in the process. I often do this after the Save () procedure to return the page to a "normal" state. This also works well if your Save program is updating the database and there are some UI elements in the page being read from the database, then you don't need to worry about reloading those elements with fresh data.

Alternatively, you can add an extension method to quickly do this:



public static class Extensions
{
    public static void Reload(this Page page)
    {
        page.Response.Redirect(page.Request.Url.AbsoluteUri);
    }
}

      

Then you call this method in your code like this:

private void SaveCrap() 
{
    SavemeBlahBlah(); // save to dbase
    this.Page.Reload(); 
}

      

+4


source


It's not a mistake. This is by design.

When you hit F5 / Refresh, it sends the same request to the server again.

+1


source







All Articles