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.