简体   繁体   English

防止用户双击ASP.NET

[英]Prevent user from double clicking in ASP.NET

I have tried the following two scripts to prevent user from submitting the below form more than one. 我尝试了以下两个脚本,以防止用户提交以下多个表单。 The submit button is being disabled in both cases, however the form is not being sent (placed a break point and the OnClick method is not being called) 在两种情况下都禁用了提交按钮,但是未发送表单(放置了一个断点并且未调用OnClick方法)

Button: 按钮:

<asp:Button ID="BtnSubmit" runat="server" Text="Submit" onclick="BtnSubmit_Click" />

Scripts: 脚本:

<script type="text/javascript">        $("#BtnSubmit").click(function () {
        var button = $(this);
        button.prop('disabled', true);
        setTimeout(function () {
            button.prop('disabled', false);
        }, 5000);
    });</script>

and

$('form').on('submit', function (e) {
$('[type="submit"]').prop('disabled','disabled');
});

One quick idea is to change the type of the button to "button" (instead of "submit"), instead of disabling it. 一种快速的想法是将按钮的类型更改为“ button”(而不是“ submit”),而不是禁用它。 That way, it won't submit the form if clicked again at least. 这样,如果至少再次单击,它将不会提交表单。

Another other idea comes from here: http://encosia.com/disable-a-button-control-during-postback/ - 另一个想法来自这里: http : //encosia.com/disable-a-button-control-during-postback/-

The important text from the link: 链接中的重要文字:

The one pitfall that comes with disabling a submit button on the client side is that it will cancel the browser's submit, and thus the postback. 禁用客户端上的提交按钮的一个陷阱是它将取消浏览器的提交,并因此取消回发。 Setting the UseSubmitBehavior property to false tells .NET to inject the necessary client script to fire the postback anyway, instead of relying on the browser's form submission behavior. UseSubmitBehavior属性设置为false可以UseSubmitBehavior .NET注入必要的客户端脚本以无论如何触发回发,而不是依赖于浏览器的表单提交行为。

Following that, your button would look something like this: 之后,您的按钮将如下所示:

<asp:Button runat="server" ID="BtnSubmit" Text="Submit"
  OnClientClick="this.disabled = true; this.value = 'Submitting...';" 
  UseSubmitBehavior="false" 
  OnClick="BtnSubmit_Click" />

I don't like the inline OnClientClick , so you should try taking it out and keeping your jQuery click handler, as well as setting the UseSubmitBehavior appropriately. 我不喜欢嵌入式OnClientClick ,因此您应该尝试将其取出并保留jQuery click处理程序,以及适当地设置UseSubmitBehavior I have a feeling the order of events shouldn't matter. 我觉得事件的顺序无关紧要。

For the former, you would have to do instead: 对于前者,您必须改为:

<script type="text/javascript">        
$("#<%= BtnSubmit.ClientID %>").click(function () {
        var button = $(this);
        button.prop('disabled', true);
        setTimeout(function () {
            button.prop('disabled', false);
        }, 5000);
    });</script>

The actual ID is going to be way longer than btnsubmit, unless you have ClientIDMode="Static" defined. 除非您定义了ClientIDMode="Static" ,否则实际的ID将比btnsubmit长得多。 (something like ct100_contentplaceholder_btnsubmit). (类似于ct100_contentplaceholder_btnsubmit)。 Using the ClientID property from the server locates the proper ID of the control. 使用服务器上的ClientID属性可以找到控件的正确ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM