如何检测是否刷新键(F5)是pressed [英] How to detect if Refresh button(F5) is pressed

查看:126
本文介绍了如何检测是否刷新键(F5)是pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有低于code

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           //do something 
        }
        else
        {
            // do something else
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //do something       
    }
}

问题的关键是,如果我preSS F5 /刷新按钮或点击一个按钮回发发生。我将如何prevent采取任何行动,如果被点击F5 /刷新按钮code?

The point is that a post back happens if I press F5/refresh button or a button click. How will I prevent the code from doing any action if F5/refresh button is clicked?

我已经检查检测F5幸福pressed和刷新,但我的解决方案将是不同的,因为我需要做这在C#code。

I have checked Detect F5 being pressed and Refresh but the solution of mine will be different as I need to do this in C# code.

感谢

推荐答案

您正试图捕获客户端上的东西 - 所以它必须是客户端脚本(如链接讨论)

You are trying to capture something on the client - so it must be client side script (as discussed in the link).

这不是一个的回发的在ASP.Net方面 - 只是简单地再次请求你的页面(GET)。你不能阻止这个 - 它只是喜欢去一些其他的页面上你的网站,并点击后面通过一些导航

It's not a postback in ASP.Net terms - your page is simply being requested again (GET). You cannot stop this - its just like going to some other page on your web site and clicking back through some navigation.

如果你说你要prevent从正在运行某些类型的服务器端code你有(超过X倍),那么你可以考虑一下会话或cookies和阅读他们之前运行任何处理。一个简单的例子:

If you are saying you want to prevent some type of server side code you have from being run (more than x times) then you can think about sessions or cookies and read them in before you run whatever process. A simplistic sample:


  • 访问页面1 - 设置会话cookie的或标识第1页进程运行

  • 访问页2 - 设置会话cookie的或标识2页过程中运行

  • 返回页面1 - 检查会话或cookie变量的存在,如果不存在,不跑第1页过程

另一种选择,如果可行的是使用ASP.Net缓存。

Another option, if viable is to use ASP.Net caching.

这篇关于如何检测是否刷新键(F5)是pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆