How to Prevent Disable Multiple Button Click in C# VB.Net

Disabling or preventing multiple button click event on pages is a common task. Specially when you want to develop an e-commerce site, client must want to disable the payment button after first click of the user. So that user can’t click twice or double or multiple time. Normally we have used an onclick javascript event to disable a button after first click. In each server side control has already an onclick event to do the same thing.

Basically my approach is different from others. Here i will register the javascript onclick attributes in the run time, means in page_load event & after that i will give the command to execute the server side event.

To understand what i am trying, look at the below image, when user click on the button i will disable the button & after completion the job the button will be enabled immediately. If you want to redirect after button click then no issue with this script.


To do the example first add the below Button:

<asp:Button runat="server" ID="cmdSubmit" Text="Clcik Me To Disable" OnClick="cmdSubmit_Click" />

Now add the onclick event under Page_Load:
C# Code:

cmdSubmit.Attributes.Add("onclick", "this.disabled=true;" + ClientScript.GetPostBackEventReference(cmdSubmit, "").ToString());

VB.Net Code:

cmdSubmit.Attributes.Add("onclick", "this.disabled=true;" + ClientScript.GetPostBackEventReference(cmdSubmit, "").ToString())

Now do some work after Button Click to Test:
C# Code:

    protected void cmdSubmit_Click(object sender, EventArgs e)
        //Using threading just to confirm it works
        Response.Write("I have clicked : " + DateTime.Now.ToString());

VB.Net Code:

    Protected Sub cmdSubmit_Click(sender As Object, e As EventArgs)
        'Using threading just to confirm it works
        Response.Write("I have clicked : " + DateTime.Now.ToString())
    End Sub

Note: Here i have used Thread to simulate the example

Hope now you can restrict or prevent the user from twice or more than one or from multiple mouse click.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, Client, Javascript, VB.Net

Leave a Reply

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