分享
 
 
 

Detecting Page Refresh

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

source:

http://stevenbey.co.uk/detectingPageRefresh.aspx

begin:

Detecting Page Refresh

Steven Bey

12.11.2004

Introduction

A common problem that Web Application Developers encounter is how to stop the user from refreshing the page. This is a problem if the previous request to the server was a PostBack, which, for example, inserted the WebForm’s data into a database. The result: duplicate rows in the database. The answer to this problem is that you can’t stop the user from refreshing the page, however there is a way to determine if this event has occurred.

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: _refreshState (which on initial page request is defaulted to false and on subsequent PostBack requests is the value of ViewState) is assigned to the Session["__ISREFRESH"] item and the negated _refreshState is saved to the new ViewState.

First of all the Session["__ISREFRESH"] item is initialised/updated with the value of _refreshState (and therefore matching it with latest page request). Secondly, an object array is created. The base class’s SaveViewState is called and the returning value is assigned to the first item in the array. The negated value of _refreshState is assigned to the second item of the arrya. Finally, the objet array is returned to the calling method.

--

Once a PostBack event takes place the LoadViewState method is called.

protected override void LoadViewState(object savedState)

{

object[] allStates = (object[]) savedState;

base.LoadViewState(allStates[0]);

_refreshState = (bool) allStates[1];

_isRefresh = _refreshState == (bool) Session["__ISREFRESH"];

}

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.

First of all savedState is converted into an object array and the first item is passed to the base class’s LoadViewState method. The value of the second item is then stored in _refreshState. Finally, _refreshState is compared to the value stored in the Session["__ISREFRESH"] item and the result stored in _isRefresh, which is then used by the IsRefresh property.

--

The listing below shows the entire class definition:

namespace StevenBey.Web.UI

{

public 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" %>

<html>

<head>

<title>Detecting Page Refresh</title>

</head>

<body>

<form runat="server">

<asp:button Text="Test Refresh" runat="server" />

</form>

IsRefresh = <%= IsRefresh %>

</body>

</html>

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.

Live Demo

Conclusion

In this article I have demonstrated a simplified method of detecting a page refresh event.

Download the files used in this this article.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有