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.
Just use the below line code under page_load event:
1 | 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:
01 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Disable_Multiple_button_click.aspx.cs" Inherits="Disable_Multiple_button_click" %> |
07 | < title >How to disable multiple button click</ title > |
10 | < form id = "form1" runat = "server" > |
12 | < asp:Button runat = "server" ID = "cmdSubmit" Text = "Clcik Me" OnClick = "cmdSubmit_Click" /> |
The complete serverside code is given below:
04 | public partial class Disable_Multiple_button_click : System.Web.UI.Page |
06 | protected void Page_Load( object sender, EventArgs e) |
08 | cmdSubmit.Attributes.Add( "onclick" , "this.disabled=true;" + ClientScript.GetPostBackEventReference(cmdSubmit, "" ).ToString()); |
10 | protected void cmdSubmit_Click( object sender, EventArgs e) |
12 | System.Threading.Thread.Sleep(5000); |
13 | 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.
ref : http://shawpnendu.blogspot.com/2010/02/how-to-disable-multiple-button-click-in.html
No comments:
Post a Comment