I have a button like the following,
<asp:Button ID="pagerLeftButton" runat="server" OnClientClick="disable(this)" onclick="pager_Left_Click" Text="<" />
When I use my button like that, onclick is not firing. When I remove OnClientClick, then onclick is firing.
What I need to do is, disable the button during the postback and enable it after the postback ends.
Edit: Additional information:
I added break point to my firing functions is c# part and I am debugging, they are not firing for sure. Those functions are like
protected void pager_Left_Click(object sender, EventArgs e)
{
//Do smthing.
}
protected void pager_Right_Click(object sender, EventArgs e)
{
//Do smthing.
}
and when I click my button, it is disabled for 1-2 seconds and automatically enabled, but I am not sure why it is enabled. I didn't add any part for it to be enabled again.
OnClientClick seems to be very picky when used with OnClick.
I tried unsuccessfully with the following use cases:
But they did not work. The following worked:
From this article on web.archive.org :
There are two issues here:
Disabling the button on the client side prevents the postback
To overcome this, disable the button after the JavaScript
onclick
event. An easy way to do this is to usesetTimeout
as suggested by this answer.Also, the
OnClientClick
code runs even if ASP.NET validation fails, so it's probably a good idea to add a check forPage_IsValid
. This ensures that the button will not be disabled if validation fails.It's neater to put all of this JavaScript code in its own function as the question shows:
Disabling the button on the client side doesn't disable it on the server side
To overcome this, disable the button on the server side. For example, in the
OnClick
event handler:Lastly, keep in mind that preventing duplicate button presses doesn't prevent two different users from submitting the same data at the same time. Make sure to account for that on the server side.
Vinay (above) gave an effective work-around. What's actually causing the button's OnClick event to not work following the OnClientClick event function is that MS has defined it where, once the button is disabled (in the function called by the OnClientClick event), the button "honors" this by not trying to complete the button's activity by calling the OnClick event's defined method.
I struggled several hours trying to figure this out. Once I removed the statement to disable the submit button (that was inside the OnClientClick function), the OnClick method was called with no further problem.
Microsoft, if you're listening, once the button is clicked it should complete it's assigned activity even if it is disabled part of the way through this activity. As long as it is not disabled when it is clicked, it should complete all assigned methods.
Your JavaScript is fine, unless you have other scripts running on this page which may corrupt everything. You may check this using Firebug.
I've now tested a bit and it really seems that ASP.net ignores disabled controls. Basically the postback is issued, but probably the framework ignores such events since it "assumes" that a disabled button cannot raise any postback and so it ignores possibly attached handlers. Now this is just my personal reasoning, one could use Reflector to check this in more depth.
As a solution you could really try to do the disabling at a later point, basically you delay the
control.disabled = "disabled"
call using a JavaTimer or some other functionality. In this way 1st the postback to the server is issued before the control is being disabled by the JavaScript function. Didn't test this but it could workWhat if you don't immediately set the button to disabled, but delay that through setTimeout? Your 'disable' function would return and the submit would continue.