5
votes

In Asp.NET aspx page, there is a button for data Save, when user click on this button, I want: 1. Disable this button immediately 2. Then call code behind function Save() to save data back to DB 3. Then enable this button again.

What I did as below:

  1. JS function in apsx:

    function DisableSave(){ saveButton = document.getElementById('<%=btnSave.ClientID%>'); saveButton.disabled=true; return false; }

  2. Button script:

    <---asp:Button ID="btnSave" OnCommand="Button_Command" CommandName="Save" runat="server" Text="Save" OnClientClick="javascript:DisableSave();"/>

  3. In code behind function Save()(supposed called by CommandName), set Save button enable back.

But When I run the code, only disable the save button, Code behind call did not happened.

How to fix it?

2
Even I remove return false; still get same result, no code behind call. IE/FF same result. - KentZhou
Hi guys, my button is inside UpdatePanel for partial update. Something looks like not as expected. - KentZhou

2 Answers

3
votes

Apparently some browsers (IE) won't process the submit action of a button if it's disabled. I tried this in Firefox and it did postback, but no luck with IE. One workaround, though not providing user feedback, would be to do OnClientClick="this.onclick= function() { return false; }". That will atleast prevent more postbacks from happening.

EDIT: onclick needed to be a function not a string.

2
votes

The problem is that disabling the button using JavaScript in the onclick event stops the postback from occurring.

A simple workaround is illustrated in this CodeProject article.