There are two problems here:
Disabling a client-side button prevents postback
To overcome this, disable the button after the onclick JavaScript event. An easy way to do this is to use setTimeout as suggested by this answer .
In addition, OnClientClick code OnClientClick executed even if ASP.NET validation fails, so it is probably a good idea to add validation for Page_IsValid . This ensures that the button will not be disabled if the verification fails.
OnClientClick="(function(button) { setTimeout(function () { if (Page_IsValid) button.disabled = true; }, 0); })(this);"
Directly put all this JavaScript code in its own function, as the question shows:
OnClientClick="disable(this);" function disable(button) { setTimeout(function () { if (Page_IsValid) button.disabled = true; }, 0); }
Disabling a button on the client side does not disable it on the server side
To overcome this, disable the server side button. For example, in the onclick event handler:
OnClick="Button1_Click" protected void Button1_Click(object sender, EventArgs e) { ((Button)sender).Enabled = false; }
Finally, keep in mind that preventing duplicate button clicks does not prevent the other two users from sending the same data at the same time. Be sure to consider this on the server side.
Sam May 29 '13 at 4:11 2013-05-29 04:11
source share