Asp – How to run client and server side code for same button

asp.net

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?

Best Answer

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.

Related Topic