Disable Button to prevent double click in ASP.Net

Last Reply 4 days ago By DanH

Posted 4 days ago

I've tried using the solution at https://www.aspsnippets.com/Articles/Disable-ASPNet-button-after-click-to-prevent-double-clicking.aspx but it isn't working in my case.  I believe the problem is that my button is in a panel that is by default not visible.  Here's the Javascript code I'm using: 

window.onbeforeunload = DisableSubmitButton();

function DisableSubmitButton() {
   var btn = document.getElementById('<%=btnSubmit.ClientID%>');

   //make sure submit button was found (it may be in a panel that has Visible set to false). If found, disable it
   if (btn) {
       btn.disabled = true;
   }

}

It isn't finding the button and disabling it when I click it.  Any ideas?

Posted 4 days ago

Hi DanH,

If you are placing the button in panel and setting its visibility false then the button wont get detected by javascript code so what you need to is use Style Attribute like below for making panel visible false by default and your javascript disable button code will work.so refer below code for making panel visible false by default and making it visible in code behind.

HTML

<div>
    <asp:Panel ID="pnlDisplay" Style="display: none" runat="server">
        I am Panel
    </asp:Panel>
</div>

and making it visible from code behind

protected void Page_Load(object sender, EventArgs e)
{
    pnlDisplay.Style.Add("display", "block");
}

 


Posted 4 days ago

Actually, I found a simpler way that doesn't require any Javascript functions nor code-behind changes:

   <asp:Button ID="btnSave" runat="server" Text="Save" 
      UseSubmitBehavior="false"
      OnClientClick="this.disabled='true';" 
   </asp:button>

Works perfectly!

I agree, here is the link: http://e-iceblue.com/free-api