简体   繁体   中英

detect page refresh jQuery or C# ASP.Net

I was searching for a way of detecting in my C# code if the page had been refreshed (f5).

First I don't know if a jQuery/JavaScript solution would be better than a fully C# ASP.Net. So... the first question would be: What would make you choose one over the other?

And the third, and most important part, is if someone would help me with this. Some good tutorial, or article… anything. I tried some solutions out there… but maybe, because of my lack of knowledge they didn't work.

Thanks in advance.


update 1

I tried to do Hightechrider solution, because it seems pretty simple to implement. So I typed this code:

public bool IsRefresh { get; set; }

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);

    IsRefresh = false;
    if (Page.Request.AppRelativeCurrentExecutionFilePath == Profiles.User.LastPageUrl &&
        !Page.IsPostBack)
        IsRefresh = true;
}

PS: The Profiles.User.LastPageUrl is a session variable that I have created for other purposes, but suites the needs of this one to. So that is not some built in property.

However, after a couple of tests I got to the conclusion that a page could be a PostBack and Refresh at the same time. Because a user could press a button and after that, hit refresh. So this scenario invalidates my previous code, because of the !Page.IsPostBack. I am still seeking for a simple straight solution to this.

As far as I know a browser refresh from most browsers doesn't send any special information to indicate the refresh button was pressed. There are some options, though, depending on exactly what you are trying to accomplish.

I suggest reading about the following and see if they help you get where you need to be:

Just a gut feeling, but the second item above is probably the path you are going to need. Sometimes the best way to solve a problem is by eliminating the problem. That is, architect the solution so that pushing refresh doesn't require special handling in the first place.

As for Javascript (Client-side) versus ASP.NET (Server-Side), I think you are likely to wind up with some combination of both, not an either or.

Put this into a base class that inherits Page, this is in VB.Net, wouldnt be too hard to convert to C#

    Private mRefreshState As Boolean
    Private mIsRefresh As Boolean

    Public ReadOnly Property IsRefreshFromPostBack() As Boolean
        Get
            Return mIsRefresh
        End Get
    End Property

    Protected Overrides Sub LoadViewState(ByVal savedState As Object)
        Dim allStates As Object() = DirectCast(savedState, Object())
        MyBase.LoadViewState(allStates(0))
        mRefreshState = Convert.ToBoolean(allStates(1))
        mIsRefresh = mRefreshState = Convert.ToBoolean(Session("__ISREFRESH"))
    End Sub

    Protected Overrides Function SaveViewState() As Object
        Session("__ISREFRESH") = mRefreshState
        Dim allStates(2) As Object
        allStates(0) = MyBase.SaveViewState()
        allStates(1) = Not mRefreshState
        Return allStates
    End Function

Then call the IsRefreshFromPostBack property to determine if F5 was pressed. Works wonders, I've used in many applications

You could perhaps remember the last page in Session state and if a request is for the same page as that you know it's been refreshed. Put this code in your base page class's OnInit method (assuming you have a base page class). Check that it's not a PostBack too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM