Sunday, February 7, 2010

How to disable multiple button click in Asp.net




Please visit my new Web Site WWW.Codedisplay.com



Disabling or preventing multiple mouse button click event on Asp.net page 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 diable the button after first click. In Asp.net each serverside control has already a onclick event to do the same thing in asp.net.

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

To understand what i am trying to do look at the below sequences where when user click on the button i will disable the button & after completion the button will enable soon. If you redirect from this page then no problem.

Prevent_multiple_click

Just use the below line code under page_load event:
cmdSubmit.Attributes.Add("onclick", "this.disabled=true;" + ClientScript.GetPostBackEventReference(cmdSubmit,"").ToString());
Where cmdSubmit is our button which we dont want to click twice or more.

Now complete ths example add a page in your project. The HTML MARKUP code is given below:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Disable_Multiple_button_click.aspx.cs" Inherits="Disable_Multiple_button_click" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>How to disable multiple button click</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button runat="server" ID="cmdSubmit" Text="Clcik Me" OnClick="cmdSubmit_Click" />
    </div>
    </form>
</body>
</html>
The complete serverside code is given below:
using System;


public partial class Disable_Multiple_button_click : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        cmdSubmit.Attributes.Add("onclick", "this.disabled=true;" + ClientScript.GetPostBackEventReference(cmdSubmit,"").ToString());
    }
    protected void cmdSubmit_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(5000);
        Response.Write("I have clicked : "+DateTime.Now.ToString());
    }
}
Hope now you can restrict or prevent the user from twice or more than one or from multiple mouse click.

3 comments:

sikat ang pinoy said...

Oh noh another version has been release this year blogengine 1.6. It has new additional features now it can prevent comment spam filter that has ability to plugin custom filtering modules.It has improve outgoing emails and more improvement and fixes for this 1.6 version.

Maximus said...

given above code is working Local but after deployment is not working on azure.

Anonymous said...

Realy work ,thanks allot
I find this solution after a lot of googling
thanks once again

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash