如何检测.NET刷新页面 [英] How to detect page refresh in .net
问题描述
我有一个 Button_click
事件。同时刷新页面的previous 回发
事件再次触发。如何识别页面刷新事件,以prevent的回发
操作?
I have a Button_click
event. While refreshing the page the previous Postback
event is triggering again. How do I identify the page refresh event to prevent the Postback
action?
我试过低于code来解决它。其实,我加入了SharePoint页面的可视web部件。添加的WebPart是一个回事件这样的!回发永远是假的每一个我添加的WebPart页面的时间,我在别的循环得到一个错误,因为对象引用空
。
I tried the below code to solve it. Actually, I am adding a visual webpart in a SharePoint page. Adding webpart is a post back event so !postback is always false each time I'm adding the webpart to page, and I'm getting an error at the else loop because the object reference is null
.
if (!IsPostBack){
ViewState["postids"] = System.Guid.NewGuid().ToString();
Cache["postid"] = ViewState["postids"].ToString();
}
else{
if (ViewState["postids"].ToString() != Cache["postid"].ToString()){
IsPageRefresh = true;
}
Cache["postid"] = System.Guid.NewGuid().ToString();
ViewState["postids"] = Cache["postid"].ToString();
}
我该如何解决这个问题?
How do I solve this problem?
推荐答案
使用ViewState的工作好了很多,我详见<一个href=\"http://geekswithblogs.net/Vipin/archive/2011/06/08/detecting-browser-refresh-from-$c$c-behind-in-.net.aspx\">here.基本上是:
using the viewstate worked a lot better for me as detailed here. Basically:
bool IsPageRefresh = false;
//this section of code checks if the page postback is due to genuine submit by user or by pressing "refresh"
if (!IsPostBack)
{
ViewState["ViewStateId"] = System.Guid.NewGuid().ToString();
Session["SessionId"] = ViewState["ViewStateId"].ToString();
}
else
{
if (ViewState["ViewStateId"].ToString() != Session["SessionId"].ToString())
{
IsPageRefresh = true;
}
Session["SessionId"] = System.Guid.NewGuid().ToString();
ViewState["ViewStateId"] = Session["SessionId"].ToString();
}
这篇关于如何检测.NET刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!