如果进行更改,则提示保存数据

发布于 2024-10-07 05:37:45 字数 151 浏览 0 评论 0原文

我正在使用 asp.net,如果用户对网页进行了更改并且意外关闭了浏览器,我需要向用户显示提示。

该页面可以是从“编辑个人资料”到“提交声明”等的任何内容。

我如何显示消息框,确保仅在进行更改时才显示它(而不是用户进行更改,然后撤消-进行更改并关闭浏览器)

I am using asp.net and I need to display a prompt to the user if they have made changes to the web page, and they accidentally close down the browser.

The page could be anything from "Edit Profile" to a "Submit a Claim" etc.

How can I can display the messagebox, ensuring that it is displayed only if changes have been made (as opposed to, the user making changes, then undo-ing the changes, and shutting down the browser)

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

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

发布评论

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

评论(3

写给空气的情书 2024-10-14 05:37:45

我过去所做的是使用一些客户端脚本,在 onbeforeunload 事件期间执行此检查......

var showPrompt=true;
var isDirty=false;
var hidePopup=false;

    function onBeforeUnload() {
        if (showPrompt) {
            if (isDirty) {
                if (hidePopup || confirm("Click ok to save your changes or click cancel to discard your changes.")) {
                    doSave();
                }
            }
        }
        showPrompt = true;
        hidePopup = false;
    }
  • 当您单击锚标记时,ShowPrompt 可以设置为 false,这不会导航您离开页面。例如 '/>
  • isDirty 可用于跟踪您何时需要保存某些内容。例如,您可以执行类似 $("input").onchange(function(){isDirty=true;}); 要跟踪撤消操作,您可能需要将 isDirty 替换为检查当前状态的函数。上次保存状态的状态。
  • HidePopup 让我们可以在不向用户确认的情况下强制保存。

What I have done in the past is use some client side scripting which does this check during the onbeforeunload event....

var showPrompt=true;
var isDirty=false;
var hidePopup=false;

    function onBeforeUnload() {
        if (showPrompt) {
            if (isDirty) {
                if (hidePopup || confirm("Click ok to save your changes or click cancel to discard your changes.")) {
                    doSave();
                }
            }
        }
        showPrompt = true;
        hidePopup = false;
    }
  • ShowPrompt can be set to false when your clicking on an anchor tag which won't navigate you away from the page. For example <a onclick='showPrompt=false' href='javascript:doX()'/>
  • isDirty can be used to track when you need to save something. You could for example do something like $("input").onchange(function(){isDirty=true;}); To track undo's you might want to replace isDirty with a function which checks the current state from the last saved state.
  • HidePopup lets us force a save without confirming to the user.
安静 2024-10-14 05:37:45

如果不了解页面上的内容,就很难触摸。如果是一些控件,您可以在页面加载时捕获值并存储它们,以便稍后进行比较。如果它是一个复杂的页面,您需要对整个视图状态进行精确比较。

通常,您可以通过在第一次进行任何更改时将布尔值设置为 TRUE 来处理这种类型的情况,并忽略用户将其更改回来的情况。如果您只是想避免意外未保存数据,那么用户应该足够聪明,知道他们已经“撤消”了更改。

That's very difficult to even touch without understanding what's on the page. If it's a few controls you capture value at page load and store them so you can later compare. If it's a complex page you'd need to do an exact comparison to the entire viewstate.

Typically you'd handle this type of situation by setting a boolean to TRUE the first time any change is made and disregard the user changing it back. If you're just trying to avoid accidential non-save of data the user should be smart enough to know they've "undone" their changes.

一个人的夜不怕黑 2024-10-14 05:37:45

您可以使用 JavaScript 来完成此操作。请参阅此问题以及前两个答案之一。

You can do this with javascript. See this question and either of the first two answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文