Skip to content

ErcanOPAK.com

  • ASP.Net WebForms
  • ASP.Net MVC
  • C#
  • SQL
  • MySQL
  • PHP
  • Devexpress
  • Reportviewer
  • About
ASP.Net MVC / ASP.Net WebForms / JavaScript

How to disable ASP.Net button after click to prevent double clicking

- 13.01.23 | 20.03.23 - ErcanOPAK

Let’s say you have 2 buttons on your web page.

<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Clicked" />
<asp:Button ID="Button2" runat="server" Text="Button2" />
</form>

Scenario 1: Disabling the specific button

<script type="text/javascript">
    function DisableButton() {
        document.getElementById("<%=Button1.ClientID %>").disabled = true;
    }
    window.onbeforeunload = DisableButton;
</script>
The DisableButton JavaScript function is used for disabling specific Button when Clicked.
Inside the DisableButton JavaScript function, the Button1 is referenced using its ID and then it is disabled by setting the JavaScript disabled property to TRUE.
The DisableButton JavaScript function is called on the onbeforeunload event which occurs just before the HTML Page is unloaded from the Browser before sending it to the Server.
Scenario 2: Disabling all the button
<script type="text/javascript">
    function DisableButtons() {
        var inputs = document.getElementsByTagName("INPUT");
        for (var i in inputs) {
            if (inputs[i].type == "button" || inputs[i].type == "submit") {
                inputs[i].disabled = true;
            }
        }
    }
    window.onbeforeunload = DisableButtons;
</script>
The DisableButtons JavaScript function is used for disabling all Buttons on the Page when any one Button is Clicked.
Inside the DisableButtons JavaScript function, all the INPUT elements are referenced and a FOR loop is executed over the referenced elements.
Inside the FOR loop, if the element is of type Button or Submit, then it is disabled by setting the JavaScript disabled property to TRUE.
The DisableButtons JavaScript function is called on the onbeforeunload event which occurs just before the HTML Page is unloaded from the Browser before sending it to the Server.
Thanks to Mudassar Ahmed Khan for this helpful article.

Related posts:

Define a Default Value when insert New Record From ASPxGridView
How to add default value for Entity Framework migrations for DateTime and Bool
Imitating a blink tag with CSS3 animations
How to use ? : if statements with Asp.Net Razor
Post Views: 16

Post navigation

What is the difference between HashSet and List in .net?
How to check if javascript is enabled on the client’s browser

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

October 2024
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
28293031  
« Sep    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (848)
  • Get the First and Last Word from a String or Sentence in SQL (756)
  • How to select distinct rows in a datatable in C# (703)
  • How to add default value for Entity Framework migrations for DateTime and Bool (584)
  • Add Constraint to SQL Table to ensure email contains @ (521)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (480)
  • How to make theater mode the default for Youtube (465)
  • Average of all values in a column that are not zero in SQL (453)
  • Find numbers with more than two decimal places in SQL (383)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (371)

Recent Posts

  • How to Reset Taskbar in Windows 11
  • Essential Steps to Take After Windows 11 Updates
  • How to list all tables referencing a table by Foreign Key in MS SQL
  • How to format date in Javascript
  • How to generate a random number for each row in T-SQL
  • How to solve ‘Microsoft.TeamFoundation.Git.Contracts.GitCheckoutConflictException’ problem
  • Why nautical mile equals 1852 mt
  • How to Find Day Name From Date in SQL Server
  • How to make pagination in MS SQL Server
  • How to update Identity Column in SQL Server

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (848)
  • Get the First and Last Word from a String or Sentence in SQL (756)
  • How to select distinct rows in a datatable in C# (703)
  • How to add default value for Entity Framework migrations for DateTime and Bool (584)
  • Add Constraint to SQL Table to ensure email contains @ (521)

Recent Posts

  • How to Reset Taskbar in Windows 11
  • Essential Steps to Take After Windows 11 Updates
  • How to list all tables referencing a table by Foreign Key in MS SQL
  • How to format date in Javascript
  • How to generate a random number for each row in T-SQL

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter

© 2024 ErcanOPAK.com

Proudly powered by WordPress | Theme: Xblog Plus by wpthemespace.com