0

for my Winform applications I have used ClickSuppressor so that pereventing multiple action by pressing button twice or more. I am wondering what about the WebForms and MVC applications? Do we also need to use ClickSuppressor in this kind of projects?

using (new MyClass.ClickSuppressor(this))
{
    try
    { 
        //my action code i.e. add record
    }
}


MyClass:

/// <summary>
/// Stops to queue event in order to prevent from "multiple click" when adding a new record.
/// </summary>
internal class ClickSuppressor : IDisposable
{
    private Control mCtrl;
    public ClickSuppressor(Control ctrl)
    {
        mCtrl = ctrl;
        mCtrl.Enabled = false;
        mCtrl.Update();
    }
    public void Dispose()
    {
        Application.DoEvents();
        /* Or this:
        MethodInfo mi = typeof(Control).GetMethod("RemovePendingMessages", BindingFlags.Instance | BindingFlags.NonPublic);
        mi.Invoke(mCtrl, new object[] { 0x201, 0x203 });
        mi.Invoke(mCtrl.Parent, new object[] { 0x201, 0x203 });
        */
        if (!mCtrl.IsDisposed) mCtrl.Enabled = true;
    }
}
Jack
  • 1
  • 21
  • 118
  • 236
  • Why would you ever want to do that? Are you targeting a special audience? (like e.g. cats) – Uwe Keim Nov 12 '13 at 10:51
  • For example when adding a record some user might press button twice or more at the same procedure. In that case it is possible multiple records with the same data might be added to the database. In order to prevent this, I used to ClickSuppressor in WinForms. – Jack Nov 12 '13 at 11:07

1 Answers1

1

This needs to be done in the browser with scripts.

Set the disabled attribute on the element to "disabled" when it is clicked:

<button class="singleClick">

The jQuery:

$(".singleClick").on("click", function(){
    this.attr("disabled", "disabled");
});

You can remove the disabled status later with:

$(element).removeAttr("disabled");
anthr
  • 719
  • 5
  • 13
  • Thanks for reply. But I need a solution without using Javascript. Is there any that can be used like my custom method above? – Jack Nov 12 '13 at 11:03
  • 1
    There is no other way to do it. Keep in mind, this is only useful if you are using async calls. If the page redirects when the button is clicked, there is no reason to do this. – anthr Nov 12 '13 at 11:07
  • Do you mean calling a method in Controller by "page redirect"? For example, I add a record in my View and call Adding method from Controller then display a status message on the same View. In that case is also it mean "a redirect to another page"? Could you clarify me pls? – Jack Nov 12 '13 at 12:03
  • Page redirect means that the browser will reload the page, or direct to a new page. If this occurs then you shouldn't worry about multiple clicks. If this is still a problem, you might want to use AJAX requests, and disable the buttons to prevent multiple requests. – anthr Nov 12 '13 at 12:50
  • Ok, in that case even if the same page reloads for example due to validation error, there is no need to use clicksuppressor. Thanks. – Jack Nov 12 '13 at 13:07