<p>I have a few controls that inherit from <code>ASP.NET buttons</code> and use <code>onserverclick</code>.</p>

<p>If the user clicks twice, the button fires two server side events. How can I prevent this?</p>

<p>I tried setting <code>this.disabled='true'</code> after the click (in the <code>onclick</code> attribute) via javascript, but that blocks the first postback as well.</p>

[{'answer_id': 50435, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 1, 'selected': False, 'text': '<p>Someone else said this somewhere on here a few days ago, and I concur - use javascript to simply <em>hide</em> the button instead of disabling it; you could show a "spinner" image in its place, which lets the user know what is going on.</p>\n'}, {'answer_id': 50439, 'author': 'Ryan Lanciaux', 'author_id': 1385358, 'author_profile': 'https://Stackoverflow.com/users/1385358', 'pm_score': 5, 'selected': True, 'text': '<p>See this example for disabling control on postback. It should help you do what you\'re trying to achieve.</p>\n\n<p><a href="http://encosia.com/2007/04/17/disable-a-button-control-during-postback/" rel="noreferrer">http://encosia.com/2007/04/17/disable-a-button-control-during-postback/</a></p>\n'}, {'answer_id': 50446, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 1, 'selected': False, 'text': '<p>Instead of hiding, what I have done is swapping buttons using javascript. Show another greyed out image on the click of the first button.</p>\n'}, {'answer_id': 50448, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': "<p>You don't necessarily want to show the button disabled on postback.  You want to make sure they don't accidentally submit twice.  So disabling or hiding the button as a result of a <em>server-side</em> action is already too late in the game.  By this point the 2nd request is already on it's way.  You need to either do it with javascript or make sure your server side code won't run twice.</p>\n"}, {'answer_id': 11466704, 'author': 'Andreas Krohn', 'author_id': 1521232, 'author_profile': 'https://Stackoverflow.com/users/1521232', 'pm_score': 2, 'selected': False, 'text': '<p>In case of an updatepanel and a button inside a FormView-Template I use the following approach:</p>\n\n<pre><code>&lt;script type="text/javascript"&gt;\n    // Using that prm reference, hook _initializeRequest\n    Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequestBuchung);\n\n    // Abfangen von Mehrfachklicks auf Buttons für asynchrone Postbacks im Updatepanel\n    function InitializeRequestBuchung(sender, args) {\n        var arrButtonIds = ["ButtonInsert", "ButtonUpdate"];\n\n        // Get a reference to the PageRequestManager.\n        var prm = Sys.WebForms.PageRequestManager.getInstance();\n        if (prm.get_isInAsyncPostBack() &amp; jQuery.inArray(args.get_postBackElement().id, arrButtonIds) &gt; -1) {\n            args.set_cancel(true);\n        }\n    }\n&lt;/script&gt;\n</code></pre>\n\n<p>This cancels a following postback if currently a async postback is still active. Works perfectly.</p>\n'}, {'answer_id': 25312370, 'author': 'latuya23', 'author_id': 1314273, 'author_profile': 'https://Stackoverflow.com/users/1314273', 'pm_score': 1, 'selected': False, 'text': '<p>Set the Button property UseSubmitBehavior to false. Then create an OnClientClick function that disables the button.\nIt would look something like this:</p>\n\n<pre><code>&lt;script type="text/javascript"&gt;\n   function disableFunctn(button){\n       button.disabled = true;\n   }\n&lt;/script&gt;\n&lt;asp:Button ID="button1" UseSubmitBehavior="false" OnClientClick="disableFunctn(this);"/&gt;\n</code></pre>\n\n<p>fastest cheapest way: </p>\n\n<pre><code>&lt;asp:Button ID="button1" UseSubmitBehavior="false" OnClientClick="this.disabled=true;"/&gt;\n</code></pre>\n'}, {'answer_id': 38317396, 'author': 'user3723972', 'author_id': 3723972, 'author_profile': 'https://Stackoverflow.com/users/3723972', 'pm_score': 0, 'selected': False, 'text': '<p>You can also try for example btnSave.Enable = false; when the button is hit and before the processing for the button is done in the Click Event routine.  If you need it to be reset to allow it to be enabled have a separate button that resets the button for reuse.  </p>\n\n<p>Another method is to set the button with verification so that the user is asked if they want to Save, it should pop up both times.</p>\n\n<p>Yet another method would be to flag the first occurrence then set a popup for the second to verify a second or subsequent usage.</p>\n'}]
['https://Stackoverflow.com/questions/50426', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1782/']