Click here to Skip to main content
15,880,427 members
Articles / Web Development / ASP.NET
Article

Disable ASP Button on Submit and capture the PostBack OnClick Event

Rate me:
Please Sign up or sign in to vote.
3.82/5 (15 votes)
9 Jun 2005 191.5K   725   50   25
This article describes how to disable a ASP button on click to prevent multiple clicks and still fire the button event on the server.

Introduction

This article describes how to disable an ASP button on click to prevent multiple clicks and still fire the button event on the server. This is a real simple solution that a colleague of mine Harpreet Chawla figured out.

The code

Just a real simple fix. Just add the event to the button attributes for the onclick event. It disables via client script the button, then returns the control to the Server button event through the GetPostBackEventReference method, which:

'Obtains a reference to a client-side script function that causes, when invoked, the server to post back to the page. This method also passes a parameter to the server control that performs the post-back processing on the server.' {MSDN Visual Studio Help}.

C#
public class WebForm1 : System.Web.UI.Page
{
    protected System.Web.UI.WebControls.Button Button1;
    
    private void Page_Load(object sender, System.EventArgs e)
    {
    
    
    }
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
        Button1.Attributes.Add("onclick","javascript:" + 
                  Button1.ClientID + ".disabled=true;" + 
                  this.GetPostBackEventReference(Button1));
        InitializeComponent();
        base.OnInit(e);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    { 
        this.Button1.Click += 
                new System.EventHandler(this.Button1_Click);
        this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    private void Button1_Click(object sender, System.EventArgs e)
    {
        System.Threading.Thread.Sleep(5000);
        //Response.Redirect("http://www.msn.com");
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Christopher G. Lasater

I am also a published author, please check out my book:
ISBN: 1-59822-031-4
Title: Design Patterns
Author:Christopher G. Lasater
More from my web site
Amazon.com


Comments and Discussions

 
GeneralA method to encapsulate all in one Pin
eatemadi5-May-11 9:33
eatemadi5-May-11 9:33 
GeneralPost back problem Pin
daakanksha9-Aug-09 23:17
daakanksha9-Aug-09 23:17 
GeneralHere is a much simplier solution... Pin
SupermanDT31-Jul-07 1:26
SupermanDT31-Jul-07 1:26 
GeneralRe: Here is a much simplier solution... Pin
stormcandi22-Aug-07 8:57
stormcandi22-Aug-07 8:57 
GeneralRe: Here is a much simplier solution... Pin
johram4-Sep-08 22:56
johram4-Sep-08 22:56 
GeneralRe: Don't work for me :-( Pin
jbu208-Sep-08 9:16
jbu208-Sep-08 9:16 
GeneralRe: Here is a much simplier solution... Pin
Ed The Fish16-Nov-08 6:35
Ed The Fish16-Nov-08 6:35 
GeneralRe: Here is a much simplier solution... Pin
SupermanDT17-Nov-08 2:16
SupermanDT17-Nov-08 2:16 
GeneralRe: Here is a much simplier solution... Pin
XX sdfsbh12-Apr-10 3:46
XX sdfsbh12-Apr-10 3:46 
GeneralSee this control as well. Pin
Tittle Joseph20-Sep-06 22:53
Tittle Joseph20-Sep-06 22:53 
GeneralGood one! Pin
argatxa5-Sep-06 8:44
argatxa5-Sep-06 8:44 
QuestionPlease help me Pin
leo237-Jun-06 14:07
leo237-Jun-06 14:07 
GeneralTypo in code Pin
code_project_user19-Apr-06 5:13
code_project_user19-Apr-06 5:13 
GeneralRe: Typo in code Pin
Christopher G. Lasater19-Apr-06 5:40
Christopher G. Lasater19-Apr-06 5:40 
Nope, doesn't need this unless you are adding something else (javascript) behind this. Of course this depends on your browser specification on javascript. Was this in response to a particular problem?

Chris Lasater
http://www.geocities.com/lasaterconsult
GeneralRe: Typo in code Pin
code_project_user19-Apr-06 8:48
code_project_user19-Apr-06 8:48 
GeneralNice but useless Pin
Nas Vanchev16-Jun-05 3:05
Nas Vanchev16-Jun-05 3:05 
GeneralRe: Nice but useless Pin
pajacobs18-Jul-05 7:55
pajacobs18-Jul-05 7:55 
GeneralNot useless, MS Preffered Pin
Christopher G. Lasater29-Mar-06 6:08
Christopher G. Lasater29-Mar-06 6:08 
GeneralRe: Not useless, MS Preffered Pin
Lokheed20-Jun-06 10:19
Lokheed20-Jun-06 10:19 
Generalhelp me im new Pin
mohammed198315-Jun-05 3:12
mohammed198315-Jun-05 3:12 
Generalhttp://aspzone.com/articles/207.aspx Pin
Anonymous12-Jun-05 0:28
Anonymous12-Jun-05 0:28 
GeneralSeems it does NOT work when verification is required Pin
zlei1210-Jun-05 17:57
zlei1210-Jun-05 17:57 
GeneralRe: Seems it does NOT work when verification is required Pin
Christopher G. Lasater13-Jun-05 4:23
Christopher G. Lasater13-Jun-05 4:23 
GeneralNice trick! Pin
Eugene Rosenfeld10-Jun-05 4:46
Eugene Rosenfeld10-Jun-05 4:46 
GeneralRe: Nice trick! Pin
Christopher G. Lasater10-Jun-05 4:49
Christopher G. Lasater10-Jun-05 4:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.