防止按 F5 时刷新页面

发布于 2024-10-03 05:23:32 字数 132 浏览 5 评论 0原文

我已经使用服务器端控件(如按钮)创建了表单..并且还在其上编写了事件.. 现在运行时,单击按钮后,我按 F5 刷新了页面。Page_load 执行正常,但 button1_click() 事件也触发...那么在这种情况下我如何停止此事件执行。请建议我

I have Created form with Server Side control like button..and also have written event on that..
Now runtime after click on the button i have refreshed the page by pressing F5.Page_load is executing fine but button1_click() event also firing...So how can i stop this event execution in this scenario.Please suggest me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

山有枢 2024-10-10 05:23:32

简短回答:不可能

较长回答:没有网站可以阻止浏览器的功能,因为这对浏览器来说是一个严重的安全问题。

Short Answer: Not possible

Longer Answer: No web site can block the browser's functionality as that would be a serious security concern for the browser.

难忘№最初的完美 2024-10-10 05:23:32

欢迎来到网页开发,没有办法阻止页面刷新和转发,这完全是浏览器行为。

不过也有解决办法;您可以做的一件简单的事情是,在处理按钮点击后,您可以将浏览器重定向回页面,这样刷新就不会包含按钮按下的重新发布。显然,这可能需要一些其他状态管理系统(而不是视图状态),也许您可​​以将状态存储在会话中。

Welcome to web development, there is no way to stop the page from refreshing and reposting, this is entirely a browser behavior.

There are work arounds though; one simple thing that you can do is after processing your button click, you can redirect the browser back to the page so that a refresh will not include a repost of your button press. Obviously this might require some other state management system (rather than view-state), maybe you can store your state in the Session.

随梦而飞# 2024-10-10 05:23:32
 <script type="text/javascript" language="javascript">
    function checkKeyCode(evt)// for F5 disable
    {

        var evt = (evt) ? evt : ((event) ? event : null);
        var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if (event.keyCode == 116)//disable F5
        {
            evt.keyCode = 0;
            return false
        }
        if (event.keyCode == 123) {
            evt.keyCode = 0;
            return false
        }
        if (event.keyCode == 93) {
            evt.keyCode = 0;
            return false
        }
        if (event.altKey == true && event.keyCode == 115) //disable alt-F4
        {
            evt.keyCode = 0;
            return false
        }



        //alert(event.keyCode);
    }

    document.onkeydown = checkKeyCode;

</script>

将此脚本发布到您的主页或您想要阻止 F5 按钮的位置。

 <script type="text/javascript" language="javascript">
    function checkKeyCode(evt)// for F5 disable
    {

        var evt = (evt) ? evt : ((event) ? event : null);
        var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if (event.keyCode == 116)//disable F5
        {
            evt.keyCode = 0;
            return false
        }
        if (event.keyCode == 123) {
            evt.keyCode = 0;
            return false
        }
        if (event.keyCode == 93) {
            evt.keyCode = 0;
            return false
        }
        if (event.altKey == true && event.keyCode == 115) //disable alt-F4
        {
            evt.keyCode = 0;
            return false
        }



        //alert(event.keyCode);
    }

    document.onkeydown = checkKeyCode;

</script>

Post this script on your master page or where u want to block F5 button.

若有似无的小暗淡 2024-10-10 05:23:32

您可能会使用:

window.onbeforeunload = function ()
{
    return false;
}

这将显示一条提示,询问用户是否想要离开,但可能不需要。 (另外,我不知道支持的程度)

You could potentially use:

window.onbeforeunload = function ()
{
    return false;
}

This will display a prompt asking the user if they want to navigate away and may not be desired. (Plus, I don't know the extent of support)

临风闻羌笛 2024-10-10 05:23:32

海报使用的是 ASP.NET WebForms。在 MVC 中,使用 Post-Redirect-Get 模式,这不会成为问题。

但自 2004 年以来,Webforms 已经解决了这个问题(甚至更早 - 我在 2001 年编写了一个简单的票证解决方案),这篇文章如下: http://msdn.microsoft.com/en-us/library/ms379557.aspx

基本上,作者创建了与每个​​请求关联的唯一票证,并删除了该票证的有效性第一次回发时的票证。任何意外(或恶意)的后续回发都会被忽略。它包含在一个基本页面中,因此应该很容易添加到任何 Webforms 解决方案中。

The poster is using ASP.NET WebForms. In MVC, using the Post-Redirect-Get pattern, this would not be a problem.

But this has been solved for Webforms since 2004 (and even before that - I wrote a simple ticket solution in 2001) with this article: http://msdn.microsoft.com/en-us/library/ms379557.aspx

Basically the author creates a unique ticket associated with each request, and removes the validity of the ticket on first postback. Any accidental (or malicious) subsequent postbacks are ignored. It's wrapped up in a base page so it should be easy to add to any Webforms solution.

浊酒尽余欢 2024-10-10 05:23:32

您想要阻止的并不是真正的页面刷新,而是页面重新POST

如前所述,这在客户端是不可能的,因为刷新将重播浏览器在上次操作中执行的操作。

但是您可以将其捕获在服务器端,因为服务器可以保存一些信息来控制请求。

最简单的方法是将某种 RequestID 放在 Page Load 上的隐藏字段中,将其存储在 Session 中的某个位置。并在下一个 POST 上控制其值以验证请求,然后覆盖它以避免使用相同的 RequestID 重新POST

也许有更好的方法:)

您的“问题”类似于用户双击恐怖行为: ASP.Net双击问题

Its not really page refresh that you want to block, its page re-POST.

As already said, it's not possible on the client side because the refresh will replay what the browser did on the last action.

But you can trap it on the server side because the server can hold some infos to control the request.

The easiest way is to put a sort of RequestID in a hidden field on Page Load, store it somewhere like in the Session. And control its value on next POST to valid the request then override it to avoid re-POST with the same RequestID.

Maybe there's a better way :)

Your 'problem' is similar to a user double-click horror behavior : ASP.Net double-click problem

绅士风度i 2024-10-10 05:23:32

单击按钮时是否必须刷新页面?您可以重新连接按钮以进行 ajax 调用来提交数据吗?如果是这样,那么您就不必担心刷新时页面重新提交。

Do you have to refresh the page on the click of the button? Could you rewire the button to make an ajax call to submit the data? If so, then you would not need to worry about the page re-submitting on refresh.

脱离于你 2024-10-10 05:23:32

F5 刷新问题的唯一解决方案是 Response.Redirect。请参阅我关于此主题的帖子。

https://stackoverflow.com/questions/12596152/asp- net-f5-browser-refresh-duplicate-record/12596153#12596153

The only solution for the F5 refresh problem is Response.Redirect. Please see my post on this topic.

https://stackoverflow.com/questions/12596152/asp-net-f5-browser-refresh-duplicate-record/12596153#12596153

來不及說愛妳 2024-10-10 05:23:32

我得到一些不允许您按 F5 和/或 Ctrl + R 的代码:

<!DOCTYPE html>
<html>
<head>
<title>
Key code test!
</title>
<script type="text/javascript">
var x, ctrlKeyPressed = false;  
function keyDown(e){            
x = e.charCode || e.keyCode;
document.getElementById("key").innerHTML = x;
if(x == 17) {
ctrlKeyPressed = true;
}
if(ctrlKeyPressed && x == 82) {
e.preventDefault();
} else if(x == 116) {
e.preventDefault();
}
}
function keyUp(e) {
x = e.charCode || e.keyCode;
if(x == 17) {
ctrlKeyPressed = false;
}
}
window.onload = function () {
document.onkeydown = function (e) {
keyDown(e);
};
document.onkeyup = function (e) {
keyUp(e);
};
}
</script>
</head>
<body>
Press a key!
<br/>
Key code: <div style="display:inline;" id="key">Nothing</div>
</body>
</html>

I got some code that doesn't allow you to press F5 and/or Ctrl + R:

<!DOCTYPE html>
<html>
<head>
<title>
Key code test!
</title>
<script type="text/javascript">
var x, ctrlKeyPressed = false;  
function keyDown(e){            
x = e.charCode || e.keyCode;
document.getElementById("key").innerHTML = x;
if(x == 17) {
ctrlKeyPressed = true;
}
if(ctrlKeyPressed && x == 82) {
e.preventDefault();
} else if(x == 116) {
e.preventDefault();
}
}
function keyUp(e) {
x = e.charCode || e.keyCode;
if(x == 17) {
ctrlKeyPressed = false;
}
}
window.onload = function () {
document.onkeydown = function (e) {
keyDown(e);
};
document.onkeyup = function (e) {
keyUp(e);
};
}
</script>
</head>
<body>
Press a key!
<br/>
Key code: <div style="display:inline;" id="key">Nothing</div>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文