How to run client and server side code for same button?
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:
JS function in apsx:
function DisableSave(){ saveButton = document.getElementById('<%=btnSave.ClientID%>'); saveButton.disabled=true; return false; }
Button script:
<---asp:Button ID="btnSave" OnCommand="Button_Command" CommandName="Save" runat="server" Text="Save" OnClientClick="javascript:DisableSave();"/>
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?
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.
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.
精彩评论