How to detect page refresh in .net

后端 未结 6 859
梦如初夏
梦如初夏 2020-12-31 03:55

I have a Button_click event. While refreshing the page the previous Postback event is triggering again. How do I identify the page refresh event to

6条回答
  •  一生所求
    2020-12-31 04:48

    If you want to detect a refresh on an HTTP GET rather than only POSTs, here's a hacky work-around that, in modern browsers, mostly works.

    Javascript:

    window.onload = function () {
        // regex for finding "loaded" query string parameter
        var qsRegex = /^(\?|.+&)loaded=\d/ig;
        if (!qsRegex.test(location.search)) {
            var loc = window.location.href + (window.location.search.length ? '&' : '?') + 'loaded=1';
            window.history.replaceState(null, document.title, loc);
        }
    };
    

    C#:

    public bool IsPageRefresh 
    {
        get
        {
            return !string.IsNullOrEmpty(Request.QueryString["loaded"]);
        }
    }
    

    When the page loads, it will change add a QueryString parameter of loaded=1 without reloading the page (again, this--window.history.replaceState--only works in post-archaic browsers). Then, when the user refreshes the page, the server can check for the presence of the loaded parameter of the query string.

    Caveat: mostly works

    The case where this doesn't work is when the user clicks the Address Bar and presses enter. That is, the server will produce a false-positive, detecting a refresh, when odds are, the user actually meant to reload the page fresh.

    Depending on your purposes, maybe this is desirable, but as a user, it would drive me crazy if I expected it to reset the page.

    I haven't put too much thought into it, but it might be possible to write some magic in order to distinguish a refresh from a reset via the address bar using any/all of:

    • SessionState (assuming SessionState is enabled) and the value of the loaded QueryString parameter
    • the window.onbeforeunload event listener
    • keyboard events (detecting F5 and Ctrl + R to quickly change the URL back to removing the loaded QueryString parameter--though this would have a false-negative for clicking the browser's refresh button)
    • cookies

    If someone does come up with a solution, I'd love to hear it.

提交回复
热议问题