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

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

问题描述

我见过其他的解决方案,如<一个href=\"http://stackoverflow.com/questions/14810195/is-there-any-way-to-block-an-aspbuttons-onclick-event-in-c-sharp-from-the-oncl\">this其中之一是pretty简单,但如果JavaScript函数并不仅仅是确认更多的('确定吗?'); ?我从来不知道什么时候会返回一个布尔值。

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.

编辑: here另一种是正确的方式做到这一点,但我希望能够用一个票友模式对话框,并有回报是点击,而不是使用浏览器的原生确认对话框。

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:我想什么我问的是,有没有办法返回一个bool一个函数的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.

下面是你如何拦截按钮的服务器端Click事件没有自己的客户端手动触发它。

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天全站免登陆