Disable Multiple Button Click in Asp.net

Problem: How to disable button immediately on user click in asp.net so that user cannot click multiple time.

Solution: To solve this problem use regular html button, including runat=”server” attribute and write the disable client-side click event which disable button immediately and server-side event which runs the actual code for click event.

To perform this task add javascript function.

Client-Side Event to disable button immediately.
<head runat=”server”>
<script language = “javascript”>
var c=0;
function DisableClick()
{
var objName = ‘Button1’;
document.getElementById(objName).disabled=true;
c=c+1;
msg = ‘Please Wait…(‘+ c +’)!’;
document.getElementById(objName).value= msg;
var t=setTimeout(‘DisableClick()’,1000);
}
</script>
</head>

Note: Replace “Button1” with object name of button control

And following button code in body.


Button Control – HTML Button instead of Asp button.
<INPUT id=”Button1″
onclick=”DisableClick();” type=”button”
value=”Submit Payment” name=”Button1″
runat=”server” onserverclick=”Button1_Click”>

Note: onclick event is calling javascript client-side function and onserverclick event is calling server-side function which performs actual task.


Server-Side Event to perform actual task
This task can be any as per your logic, for an example I am performing some heavy time consuming task, you can even make use of Threading concept to minimize code…

protected void Button1_Click(object sender, EventArgs e)
{
ArrayList a = new ArrayList();
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < 1000; j++)
{
a.Add(i.ToString() + j.ToString());
}
}
Response.Write(“I am done: ” +
DateTime.Now.ToLongTimeString());
}

Before Button Click


During Button Click

After Task is done

Link: http://dotnetguts.blogspot.com/2008/05/disable-multiple-button-click-in-aspnet.html

 

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s