Home > ASP.Net, Javascript > Disable Button after one Click to prevent Click Twice

Disable Button after one Click to prevent Click Twice


Hi all,

In previous posts i show how to disable button after one click to prevent multiple click

Disable a Button When Clicked once on an ASP.NET Page Containing Validation Controls

Disable a button after single click

Disable button onclientclick twice ASP.Net

now try this way to disable Button after one click :-

add this Javascript Code :-


<script language="javascript" type="text/javascript">
    function disableButton(sender,group)
    {
        Page_ClientValidate(group);
        if (Page_IsValid)
        {
            sender.disabled = "disabled";
            __doPostBack(sender.name, '');
        }
    }
</script>

and in Html

<div>
      Enter Name :-
      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
          ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator><br />
    <asp:Button runat="server" ID="btnSave" Text="Save" OnClick="Save" OnClientClick="disableButton(this,'')" UseSubmitBehavior="false" />
   </div>

If there is Validation Group it will be like this :-

Add this Javascript code :-

<script language="javascript" type="text/javascript">
    function disableButton(sender,group)
    {
        Page_ClientValidate(group);
        if (Page_IsValid)
        {
            sender.disabled = "disabled";
            __doPostBack(sender.name, '');
        }
    }
</script>

and HTML will be :-

<div>
      Enter Name :-
      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ValidationGroup="i"
          ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>&nbsp;<br />
    <asp:Button runat="server" ID="btnSave" Text="Save" OnClick="Save" OnClientClick="disableButton(this,'i')" UseSubmitBehavior="false" ValidationGroup="i" />
   </div>

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. Rajashekar
    March 5, 2012 at 8:03 am

    I used same script above. But my button is not getting disabled after one click am able to click on it multiple times. One thing to note here is my asp:LinkButton is in update panel. Please help me out in fixing this issue.

  2. prasad
    May 8, 2012 at 2:26 pm

    thank u it is helpful to me really thank u……..byyyyyyy

  1. April 10, 2013 at 9:01 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: