如何在Asp.Net中禁用按钮控件上的Onbeforeunload事件? [英] How Do I Disable Onbeforeunload Event On Button Controls In Asp.Net?

查看:52
本文介绍了如何在Asp.Net中禁用按钮控件上的Onbeforeunload事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个应用程序,我使用母版页来获得常见的外观和感觉,并且我已将onbeforeunload事件添加到body元素以显示在用户导航到其他页面之前发出警报。



但onbeforeunload事件也会在按钮点击时触发。我想在页面中限制此事件按钮点击..



请建议任何解决方案。

i已添加以下代码用于onbeforeunload事件。 。

Hi All,

I have a application where i have used master page for common look and feel and i have added onbeforeunload event to the body element to show the alert before user navigates to other page.

But the onbeforeunload event is fired on button click too. i want to restrict this event button click in the page..

Please suggest any solution.
i have added below code for onbeforeunload event..

function pageunload() {
var check = true;           
           
            var isDirty = '<%=  isDirty %>';

          if (isDirty != null ) {
              if ((isDirty == "true" || isDirty == "True") && check == true) {
                  check = false;
                  return "changes have been made, click cancel and then save if you wish to keep these changes";
              }
              else if (check == false) {
                  check = true;
              }
          }
}





提前致谢。



Thanks in advance.

推荐答案

使你的isDirty页面级而不是你的函数本地化



Make your isDirty page-level rather than local to your function

var isDirty = '<%=  isDirty %>';
function pageunload() {
    var check = true;





现在添加客户端点击甚至到你的按钮,将isDirty设置为false。这将阻止您的代码触发警报。



我认为您还有其他问题,我不认为您的代码正在按照您的想法行事。 <%= isDirty%> line没有对页面上的内容做出反应,您的.net代码未在浏览器中运行。查看页面的源代码,您将看到js isDirty变量被硬编码为服务器在首次呈现页面时的脏变量值。



Now add a client side "click" even to your buttons that sets isDirty to "false". That will stop your code firing the alert.

I think you have other problems though, I don't think your code is doing what you think it is. The <%= isDirty %> line isn't reacting to what is on the page, your .net code isn't running inside your browser. View the source of the page and you'll see the js isDirty variable is hard-coded to whatever value the server isDirty variable had when the page was first rendered.


这篇关于如何在Asp.Net中禁用按钮控件上的Onbeforeunload事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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