views:

989

answers:

3

Hello,

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.

A: 

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.

Hightechrider
i think this solution still has a problem, because the page could be postback and refresh ath the same time. For example: if a user makes a postback, and refreshes after that... now the request will be a postback and refresh.
Marco
True, but you could handle that by remembering two values in Session: the last page, and whether it was a postback or not. Your question is somewhat unusual - why do you want to do this?
Hightechrider
thx 4 u'r response. The reason is: I have a page where the user updates the status of a record. And he clicks a button that says "change status", after that click i present him a modal dialog saying: "status updated to public". if the user refreshes this page, it won't make sense to see "status updated to public" once again.
Marco
+1  A: 

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.

JohnFx
A: 

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

Jason Jong