如何在关闭应用程序使用母版页的asp.net中的浏览器窗口之前提示用户. [英] How to prompt user before closing the browser window in asp.net where application is using Masterpages.

查看:51
本文介绍了如何在关闭应用程序使用母版页的asp.net中的浏览器窗口之前提示用户.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在关闭应用程序使用母版页的asp.net中的浏览器窗口之前提示用户,否则应终止用户的会话.

它应该提示类似您确定要从当前应用程序退出吗?"的提示,如果用户单击取消",则应将它返回到应用程序,就像以前一样,如果用户单击确定",则应该终止用户会话,并将相应的更改带入数据库.

注意:在某些情况下,每当页面卸载时,应用程序都会提示相同的消息,即Window.unload.

How to prompt user before closing the browser window in asp.net where application is using Master pages or instead the user''s session should be killed.

It should prompt some thing like " Are you sure you want to leave from the current application? " where if the user click''s on "cancel" then it should be returned to the application as it was before and if the user clicks on "ok" then user session should be killed and respective changes should be brought to the database.

Note : In some scenarios the application prompts the same message when ever the page gets UnLoad i.e Window.unload.

推荐答案

您将onbeforeunload事件添加到body元素:onbeforeunload ="beforeUnloadForm();"

这将变成:
You add an onbeforeunload event to your body element: onbeforeunload="beforeUnloadForm();"

This would become like:
<body onload="initForm();" onbeforeunload="beforeUnloadForm();">



然后在javascript函数中输入以下内容:



and in the javascript function you put something like this:

function beforeUnloadForm()
{
   if (window.hasChangesPending)
   {
      window.event.returnValue = "Pending changes have not been saved.";
   }
}



尝试离开页面或关闭浏览器时,用户现在必须确认.

祝你好运!

附言我不知道在用户确认后您是否有机会执行代码.但是您可以将其发送到服务器并在一段时间后处理它,如果用户单击取消,则将其丢弃.



When trying to leave the page or close the browser the user now must confirm.

Good luck!

Ps. I do not know if you get a chance to execute code after the user confirmed. But you could already send it to the server and handle it after a certain time out or discard if the user hits cancel.


这篇关于如何在关闭应用程序使用母版页的asp.net中的浏览器窗口之前提示用户.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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