January 5, 2009  
  You are here:  ArticlesASP.NET ArticlesPreventing Users From Clicking Buttons

QTCommunicationSimplified50.gif


Try Radicomm's Quicktalk, the first enterprise-class Push-To-Talk application for your Motorola/Symbol rugged devices, in your distribution center for free.

Click here to enter the 21st Century.


ASP.NET: Preventing Users From Clicking Buttons More Than Once
 

Ever had a button that caused serious problems if the user clicked it more than once?  While working on my intranet site on an order page, I had an issue where some users became impatient and would click it more than once.  So, I found some code on the internet somewhere and made a function out of it.  I call it from Page_Load().

Below is the function and an example calling it:

 

 
Article Source
 
public static void MakeButtonSingleClick(System.Web.UI.WebControls.Button button, string buttonText)
{
    System.Text.StringBuilder sbValid = new System.Text.StringBuilder(); 
    sbValid.Append("if (typeof(Page_ClientValidate) == 'function') { "); 
    sbValid.Append("if (Page_ClientValidate() == false) { return false; }} "); 
    sbValid.Append(string.Format("this.value = '{0}';", buttonText));
    sbValid.Append("this.disabled = true;"); 
    sbValid.Append(string.Format("document.all.{0}.disabled = true;", button.ClientID)); 
    //GetPostBackEventReference obtains a reference to a client-side script function 
    //that causes the server to post back to the page. 
    sbValid.Append(button.Page.GetPostBackEventReference(button)); 
    sbValid.Append(";"); 
    button.Attributes.Add("onclick", sbValid.ToString()); 
}


private void Page_Load(object sender, System.EventArgs e)
{
    ...
    ControlUtils.MakeButtonSingleClick(OrderPartButton, "Order in progress, please wait ...");
    ...
    if (!Page.IsPostBack)
    ...

}


 
Article Comments
 
DateNameComment

Add Your Comment

 

 
    

  Home|Freebies|Blog|Services|Articles|ASP.NET Depot|DotNetNuke Central|Contact Us
  Copyright 2005 Netsplore Terms Of Use Privacy Statement