Click here to Skip to main content
Click here to Skip to main content

Disable ASP Button on Submit and capture the PostBack OnClick Event

, 9 Jun 2005
Rate this:
Please Sign up or sign in to vote.
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}.

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);
    }
    
    /// <span class="code-SummaryComment"><summary>
</span>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// <span class="code-SummaryComment"></summary>
</span>
    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

Share

About the Author

Christopher G. Lasater
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 Pinmembereatemadi5-May-11 9:33 
GeneralPost back problem Pinmemberdaakanksha9-Aug-09 23:17 
GeneralHere is a much simplier solution... PinmemberSupermanDT31-Jul-07 1:26 
GeneralRe: Here is a much simplier solution... Pinmemberstormcandi22-Aug-07 8:57 
GeneralRe: Here is a much simplier solution... Pinmemberjohram4-Sep-08 22:56 
GeneralRe: Don't work for me :-( Pinmemberjbu208-Sep-08 9:16 
GeneralRe: Here is a much simplier solution... PinmemberEd The Fish16-Nov-08 6:35 
GeneralRe: Here is a much simplier solution... PinmemberSupermanDT17-Nov-08 2:16 
GeneralRe: Here is a much simplier solution... PinmemberXX sdfsbh12-Apr-10 3:46 
GeneralSee this control as well. PinmemberTittle Joseph20-Sep-06 22:53 
GeneralGood one! Pinmemberargatxa5-Sep-06 8:44 
QuestionPlease help me Pinmemberleo237-Jun-06 14:07 
GeneralTypo in code Pinmembercode_project_user19-Apr-06 5:13 
GeneralRe: Typo in code Pinmemberchris lasater19-Apr-06 5:40 
GeneralRe: Typo in code Pinmembercode_project_user19-Apr-06 8:48 
GeneralNice but useless PinmemberF1Nas16-Jun-05 3:05 
GeneralRe: Nice but useless Pinmemberpajacobs18-Jul-05 7:55 
GeneralNot useless, MS Preffered Pinmemberchris lasater29-Mar-06 6:08 
GeneralRe: Not useless, MS Preffered PinmemberLokheed20-Jun-06 10:19 
Generalhelp me im new Pinmembermohammed198315-Jun-05 3:12 
Generalhttp://aspzone.com/articles/207.aspx PinsussAnonymous12-Jun-05 0:28 
GeneralSeems it does NOT work when verification is required Pinmemberzlei1210-Jun-05 17:57 
GeneralRe: Seems it does NOT work when verification is required Pinmemberchris lasater13-Jun-05 4:23 
GeneralNice trick! PinsussEugene Rosenfeld10-Jun-05 4:46 
GeneralRe: Nice trick! Pinmemberchris lasater10-Jun-05 4:49 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 9 Jun 2005
Article Copyright 2005 by Christopher G. Lasater
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid