Search code examples
javascriptlinkbutton

How to disable a linkbutton to prevent multiple submits


I have HTML code like this:

<asp:LinkButton ID="AddButton" runat="server" OnClick="AddPatientBtn_Click">
<span class="Normal">Add</span>
</asp:LinkButton>

I find if we click 'Add' several times, it will add several patient records, so I want to change the code like this:

<asp:LinkButton ID="AddButton" runat="server" OnClick="AddPatientBtn_Click" OnClientClick="return DisableButton(this)">
<span class="Normal">Add</span>
</asp:LinkButton>

js:

function DisableButton(button) {

document.all("AddButton").click;
button.href = "javascript:void(0);";
button.setAttribute("disabled", "disabled");

}

But this doesn't work; I can still add many patient records.

What should I do?


Solution

  • Thanks for all of you!

    I find that LinkButton may not have the disable attribute, it is a <a> in html page.

    so I use another solution

    <asp:LinkButton ID="AddButton" runat="server" OnClick="AddPatientBtn_Click" OnClientClick="return DisableButton()">
    <span class="Normal">Add</span>
    </asp:LinkButton>
    

    js:

    var pending = false;
    function DisableButton() {if (pending) {
        alert("The new patient is being created. Please wait...");
        return false;
        }
        else {
             if (Page_ClientValidate(""))
             pending = true;
             return true;
         } 
    }