首页 技术 正文
技术 2022年11月8日
0 收藏 407 点赞 1,161 浏览 2011 个字

前段时间遇到了需要禁用刷新的需求,f5按钮就不说了,简单的js就能把它禁用,但是工具条上的刷新按钮却傻傻干不掉。

如果简单的在刷新时重新加载画面,通过window.location.href=”url”可以很容易的实现,但是需求是要求在刷新时什么都不做,保留画面的状态,这下子可就复杂化了。

asp.net中分辨请求是重新请求还是通过刷新按钮再次请求不是很方便,为了实现这个效果,试过了很多的方式,一下面的两种为例

private bool pageRefreshed = false; //页面是否刷新提交
private bool refreshState = false; //ViewState中暂存的状态

然后重写Page的LoadViewState与SaveViewState方法:

protected override void LoadViewState(object savedState)
{
object[] states = (object[])savedState;
base.LoadViewState(states[]);
refreshState = (bool)states[];
if(Session["__PAGE_REFRESHED"] == null)
pageRefreshed = false;
else
pageRefreshed = refreshState != (bool)Session["__PAGE_REFRESHED"];
}protected override object SaveViewState()
{
Session["__PAGE_REFRESHED"] = !refreshState;
object[] states = new object[];
states[] = base.SaveViewState();
states[] = !refreshState;
return states;
}
private void Button1_Click(object sender, EventArgs e)
{
if (pageRefreshed )
{
label.Text="this is refreshed function";
}
else
{
label.Text="this is new request function";
}
}

这种方法虽然能够实现,但是在某些请款下不适应。如果画面上同时存在文本框和按钮式,设置按钮的autopostback=”True”时,在修改完文本框的值,直接点击按钮(在文本框没有失去焦点时,直接点击按钮),这时的执行顺序是textchanged→textchanged→buttonclick,在第一次textchanged时,就把状态已经变成了true,按钮的不能执行。

2.codeproject找到了另外一种解决方法 原文地址:http://www.codeproject.com/Articles/18841/Refresh-Module

这种方式能够准确的判断是否是通过浏览器的刷新按钮进行的请求,而且使用起来也非常简单!

ddl位置:http://files.cnblogs.com/linyijia/RefreshModule.rar

1.引用dll,修改配置文件

在配置文件中添加modules

<system.web>
<httpModules>
<add name="RefreshModule"
type="RefreshModule.Module, RefreshModule"/>
</httpModules>
</system.web>

PS:wbapplication的情况下需要改成在system.webServer的modules的节点下追加modules

2.定义刷新时的行为

[Refresh()]
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack && !RefereshHelper.IsPageRefreshed)
{
// do some work with the submitted date
}
else
{ // do some work when the page is loaded with the GET method
}
}
}
   protected void Btn_AllAudit_Click(object sender, EventArgs e)
{
if (RefreshModule.RefreshHelper.IsPageRefreshed)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('请别重复提交页面!');", true);
return;
}
}

PS:如果是win7 使用本地IIS运行 需要把网站对应的程序池  基本设置里面 把集成模式 改成经典模式就可以了

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,946
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,472
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,285
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,101
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,733
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,768