拦截按钮回发的最佳方法

Best way to intercept a Button postback(拦截按钮回发的最佳方法)

本文介绍了拦截按钮回发的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过其他解决方案,例如 this 一个非常简单的方法,但是如果 javascript 函数不仅仅是 confirm('sure?'); 会怎样?我不知道它什么时候会返回一个布尔值.

I've seen other solutions like this one which is pretty straightforward but what if the javascript function does more than just confirm('sure?');? I never know when it will return a bool.

所以我决定像这样实现 所有我的 ASP.NET 按钮:

So I've decided to implement all my ASP.NET Buttons like this:

<button id="btnDelete" name="btnDelete" class="btn">Delete</button>
<asp:Button ID="_btnDelete" runat="server" OnClick="_btnDelete_Click" style="display:none;" />

$('#btnDelete').click(function (e) {
    $.blockUI({ message: $('#divConfirmDeleteModal'), overlayCSS: { cursor: 'default' }, css: { cursor: 'default' }, baseZ: 5555 });
    return false;
});

$('#btnDeleteYes').click(function () {
    $('#<%=_btnDelete.ClientID%>').trigger('click');
});

$('#<%=_btnDelete.ClientID%>').click(function (e) { 
    // the delay happens here. if i put an alert here, it fires,
    // but then the page just loads until eventually the method gets called
    <%= ClientScript.GetPostBackEventReference(_btnDelete, string.Empty) %>; 
});

..直到现在它运行良好了一段时间.我在 IE(甚至是版本 10)中遇到问题 - _btnDelete_Click 在单击最终触发它的按钮后最多需要 2 分钟才能被调用.

..and it's worked well for a while until now. I'm experiencing issues in IE (even version 10) - the _btnDelete_Click will take up to 2 minutes to get called after clicking the button that ultimately triggers it.

感觉太老套了,我想知道是否有更好的方法.

It feels too hacky and I was wondering if there is a better approach.

这里是另一个正确"的方法,但我希望能够使用更高级的模态对话框并在是"单击时返回 true,而不是使用浏览器的本机确认对话框.

edit: here is another "correct" way to do it but I want to be able to use a fancier modal dialog and have that return true on a "yes" click, rather than using the browser's native confirm dialog.

edit2:我想我要问的是,有没有办法为 OnClientClick 函数返回一个布尔值,该函数不止一件事

edit2: I suppose what I'm asking is, is there a way to return a bool for an OnClientClick function that does more than one thing

推荐答案

如果您在客户端脚本中手动触发点击事件,您的应用程序将无法通过移动浏览器的Go"按钮正常运行.

If you manually trigger click event in client script, your application won't be able function properly with mobile browser's Go button.

这里是你可以如何拦截按钮的服务器端点击事件,而无需在客户端手动触发它.

Here is how you can intercept the button's server side click event without manually triggering it by yourself at client side.

<script type="text/javascript">
    function clientClick() {
        if (confirm("Are you sure you want to delete?")) {
            // Do something at client side before posting back to server
            return true;
        }
        return false;
    }
</script>

<asp:Button runat="server" ID="DeleteButton" Text="Delete" 
    OnClick="DeleteButton_Click" OnClientClick="return clientClick();" />

你可以用 ASP.Net MVC 做很多花哨的事情.但是,它们在传统的 ASP.Net 中产生了很多问题.

You can do a lot of fancy stuffs with ASP.Net MVC. However, they create a lot of problem in traditional ASP.Net.

这篇关于拦截按钮回发的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:拦截按钮回发的最佳方法

基础教程推荐