Detect Page Refresh C# (after Post methods)

posted by nblevins on Monday, June 09, 2008


Okay...  So you are working on this great site.  You have all your DataTables set and your are just churning away, inserting records and keeping track of data.  Then, as you look through your database, you realize that you have duplicate records everywhere.  After grinding your code, you realize that is not really a programing error, but it is simply your "wonderful" users refreshing the stupid page, or navigating using the back button. Arggg!  Programming would be awesome w/o users!  lol.

This is really a situation that all programmers of all languages have to face.  Thankfully, if you are a .Net programmer, you have a pretty good option to get around this.  After googling a bit, I found all sorts of ideas / work arounds, provided by a great assortment of folks ranging from Dino Esposito to just random people, like me. 

Anyway, here is the basic concept.  Whenever a page is first loaded, the SaveViewState method is called.  This method is only called when the page is loaded.  After that, the LoadViewState is called on every page refresh.  What this means, is that you have an overwritable method that is only called on the page's first and another that is called every time that page is requested.  Lets abuse that! 

DISCLAIMER - I am piggiebacking on quite a few different people here and using pieces of their code.  I am having a bit of trouble finding the sources right now, but I will post them as soon as I find them.

Anyway... the code:

<code type="c#">

private bool _refreshState;
private bool _isRefresh;

public bool IsRefresh
{
    get
    {
        return _isRefresh;
    }
}

protected override void LoadViewState(object savedState)
{
    object[] allStates = (object[])savedState;
    base.LoadViewState(allStates[0]);
    _refreshState = (bool)allStates[1];
    _isRefresh = _refreshState == (bool)Session["__ISREFRESH"];
}

protected override object SaveViewState()
{
    Session["__ISREFRESH"] = _refreshState;
    object[] allStates = new object[2];
    allStates[0] = base.SaveViewState();
    allStates[1] = !_refreshState;
    return allStates;
}

</code>

Essentially, this bit of code sticks a var into the session, comparing the value every time the page is "refreshed" and resetting the value when a the SaveState is called (after insertions and actual postbacks).

Now, the main question is where to put it.  Really you have 2 options.  If you are just wanting this functionality on 1 page, overwrite the page object and make sure your target page inherits your new page object rather than from the standard one.  In my case, I want this functionality everywhere.  Therefore, I overwrote the Master page object and made sure my masterpage inherited from my custom object.  Consequently, you can reference your page's master page and that nice "IsRefresh" boolean will be there as one of the MasterPage's properties.  Very cool!

If you a Web User Control user (like me).  You can still use this.  Unfortunately, you will have to assume that your master page of the control will be present and will be the Custom page you created.  If you can make that leap logic, you simply need to grab the Page object and its MasterPage. 

<code type="c#">


((CustomMasterPage)Page.Master).IsRefresh

</code>

Hope this works well for everyone.  It has done wonders for me, when I needed it.



Comments

Monday, October 06, 2008
HI,

It is wonderfull article. i have implemeted and it is worked well. but why should we need to create one custom object and inherited into master page instead of writing code directly into master page?

please suggest.
Comment By: Jothi

@Jothi
Tuesday, October 07, 2008
The main reason for not being able to write the code directly into the master page is that we are having to override the LoadViewState(). Since we are having to override the method, we will have to make a custom class so that we can do the IsRefresh trick and then continue on w/ page processing as usual. The purpose for the override is that Asp.Net will call that particular function on the page object every page refresh, which allows us to 'hook' into the page process w/ relative ease. Consequently, we have to modify that function, which can only be done through inheritence and overriding.

Comment By: Nathan

This logic won't work in mozilla 3
Thursday, January 08, 2009
Comment By: ketan mehta

Wednesday, February 25, 2009
i am also searching for this concept i tried something different as followingly

declare the static bool variable in class scope and assign there only as false. check the bool variable and allow to execute further statement if its false or else return.

please give your feed back

Comment By: govind

Monday, July 19, 2010
Can u show the code creating the custom object from which the masterpage will be inherited
Comment By: jollie

Name:
Email (not shown):
Home Page (optional):
Title:

Sorry, I don't support html entry... ;)