Write an Article Write a Tutorial Add a Fast Code* *Fast Code might be any helpful code with brief comment
Stem Cells Blood Banks Insurances .Net Hosting Credit Cards PC Rental Business
Tutorials: ASP .NET C# .NET VB .NET Articles: ASP .NET C# .NET VB .NET Fast Code: ASP .NET C# .NET VB .NET
In his article “Build Your ASP.NET Pages on a Richer Bedrock” Dino Esposito outlined a mechanism to detect a page refresh. This method is cumbersome and more complicated than necessary, although the fundamental idea is sound and forms the basis of this solution. Dino’s mechanism uses a counter stored on the page and a session variable to store the previous request’s counter on the server, if the two match then we have a page refresh.
A Simpler Method : Keeping the whole process within a base Page Class ensures that the mechanism is completely encapsulated (and simple to implement) and if we use ViewState we eliminate the need to use an additional hidden field. Also, as we simply want to test if the two storage devices contain the same value, we can use two boolean variables, which further simplifies the process. The last decision to make is where, in the Page’s lifecycle, should the process take place. As we are using ViewState it would seem logical to perform the operation in the LoadViewState and SaveViewState methods. Using these two methods, and not the OnLoad method, has further benefits in that it eliminates potential problems with sub-classes implementing Page_Load.
How The Process Works : The LoadViewState method, which is part of the Page’s initialisation phase, is only invoked during PostBack and therefore SaveViewState is the only method, of the two ViewState related methods, to be called when the page is first requested.
protected override object SaveViewState() { Session["__ISREFRESH"] = _refreshState; object[] allStates = new object[2]; allStates[0] = base.SaveViewState(); allStates[1] = !_refreshState; return allStates; }
Note: The _refreshState is retrieved from ViewState and compared with the value in the Session["__ISREFRESH"] item. The result is stored in _isRefresh, which is used by the IsRefresh Property. The listing below shows the entire class definition:
namespace StevenBey.Web.UI { public partial class Page : System.Web.UI.Page { 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]); _refrehState = (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; } } }
Testing The Process:
<%@ Page Inherits="StevenBey.Web.UI.Page" %> IsRefresh = <%= IsRefresh %>
Clicking the “Test Refresh” button invokes a PostBack, however the value of IsRefresh doesn’t change until you click on the browser’s Refresh button or press F5 on the keyboard (and then click “Retry”). Clicking the “Test Refresh” button once again resets the value of IsRefresh to false.
Line 1: namespace StevenBey.Web.UI Line 2: { Line 3: public class Page : System.Web.UI.Page Line 4: { Line 5: private bool _refreshState;
28/12/2006 17:50:22 UTC
I fixed the above error.
I was wondering if you could explain more on testing the page part. Are you want us to insert a button.Because all I see in the page is this IsRefresh = False even if I refresh the page or not refresh the page.
Thanks
28/12/2006 18:17:55 UTC
This only gives us an Idea how it works. How we are going to need is some thing different. You need not to show any thing on the page.
On the Page_Load event you can Check if the Page is refreshed or not. If refreshed then dont do update.
28/12/2006 18:49:56 UTC
19/01/2007 02:08:18 UTC
Ask questions in the forum. This way it will be viewed and answerd faster. This section is only for comments.
22/01/2007 18:27:34 UTC
02/04/2007 22:32:48 UTC
10/12/2007 00:07:33 UTC
hi, i am facing something different issue, can anybody help me. After save the Data into Sql Tables calling redirect to other page. I want to clear all the controls in the page before redirecting the page. "Save" button click event is the only event firing here.
03/01/2008 22:24:35 UTC
26/03/2008 21:56:49 UTC
24/05/2008 00:03:28 UTC
26/05/2008 01:20:51 UTC
{
}
_refreshState = (
_isRefresh = _refreshState == (
Session[
allStates[0] =
allStates[1] = !_refreshState;
Response.Write(
15/07/2008 04:12:09 UTC
09/09/2008 03:41:53 UTC
22/09/2008 05:12:20 UTC
03/10/2008 08:49:40 UTC
25/11/2008 00:40:49 UTC
18/12/2008 09:44:17 UTC