ASP.NET prevent double clicking

人走茶凉 提交于 2019-12-04 21:40:12

If this is for a form post, then you can disable all submit buttons on submit with:


$('#someForm').submit(function(){
    $('input[type=submit]', this).attr('disabled', 'disabled');
});

Or are they allowed to click more than once, but only after some time has elapsed?

There are scenarios where one() fails - see the comments on http://api.jquery.com/one/ Any idea if its a browser compat issue with your users or jQuery not loading?

EDIT: Just found out this is a linkbutton. in that case try


$('#yourClientIdForYourLinkButton').click(function(e) {
    e.preventDefault();
});

or using the clientid directly something like


$('#<%=linkButton.ClientId%>').click(function(e) {
    e.preventDefault();
});

I have tried many options with JQuery but this is how I solved after writing a simple code in c#:

.aspx page:

Now you add this to your code behind in Page_Load:

       protected void Page_Load(object sender, EventArgs e)
       {

         ClientScriptManager cs = Page.ClientScript;

         ButtonID.Attributes.Add("onClick", "this.disabled=true;" +    cs.GetPostBackEventReference(ButtonID, "").ToString());

       }

and the last thing:

      protected void ButtonID_Click(object sender, EventArgs e)  
      {
            System.Threading.Thread.Sleep(5000);
            //rest of your code

      }

Hope this helps!

Update on post:

Using JavaScript you can stop form submission at client side event of button click, when it happens for second time

    <script type="text/javascript">

      var isSubmitted = false;

      function preventMultipleSubmissions() {

      if (!isSubmitted) {

         $('#<%=btnSubmit.ClientID %>').val('Submitting.. Plz Wait..');

        isSubmitted = true;

        return true;

      }

      else {

        return false;

      }

    }

    </script>
    <asp:LinkButton ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return preventMultipleSubmissions();"  OnClick="btnSubmit_Click"  />

Or you can do it in the backend on Postback:

btnSubmit.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnSubmit, null) + ";");

You can hide the existing link and place some text afterwards that alerts that user that the page is loading.

<asp:LinkButton CssClass="Once" runat="server">Button</asp:LinkButton>

<script type="text/javascript">

    $(document).ready(function ()
    {
        $("a.Once").one("click", function (event)
        {
            $(this).hide().after("Please wait while we load your page...");
        });
    });

</script>

Also, consider adding code server side. You can never count on JavaScript being enabled.

EDIT: Answer 2.0

It really depends on your desire here. One option is to disable the anchor tag after it is clicked. This should be something that works across all browsers.

Another option that I have seen used is to hide the item in question, ala http://www.ilovecolors.com.ar/avoid-double-click-jquery/, but that may be overkill for what you are attempting.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!