拦截按钮回发的最佳方式 [英] Best way to intercept a Button postback

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

问题描述

我看过其他解决方案,如这是一个非常简单的一个,但是如果javascript函数不仅仅是确认('肯定'); ?我不知道什么时候会返回一个bool。

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函数返回一个bool,该函数执行多个事情

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.

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

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