如果进行更改,则提示保存数据
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我过去所做的是使用一些客户端脚本,在 onbeforeunload 事件期间执行此检查......
'/>
$("input").onchange(function(){isDirty=true;});
要跟踪撤消操作,您可能需要将 isDirty 替换为检查当前状态的函数。上次保存状态的状态。What I have done in the past is use some client side scripting which does this check during the onbeforeunload event....
<a onclick='showPrompt=false' href='javascript:doX()'/>
$("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.如果不了解页面上的内容,就很难触摸。如果是一些控件,您可以在页面加载时捕获值并存储它们,以便稍后进行比较。如果它是一个复杂的页面,您需要对整个视图状态进行精确比较。
通常,您可以通过在第一次进行任何更改时将布尔值设置为 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.
您可以使用 JavaScript 来完成此操作。请参阅此问题以及前两个答案之一。
You can do this with javascript. See this question and either of the first two answers.