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

By , 9 Jun 2005
 

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);
    }
    
    /// <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

About the Author

Christopher G. Lasater
Web Developer
United States United States
Member
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


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralA method to encapsulate all in onemembereatemadi5 May '11 - 9:33 
GeneralPost back problemmemberdaakanksha9 Aug '09 - 23:17 
GeneralHere is a much simplier solution...memberSupermanDT31 Jul '07 - 1:26 
GeneralRe: Here is a much simplier solution...memberstormcandi22 Aug '07 - 8:57 
GeneralRe: Here is a much simplier solution...memberjohram4 Sep '08 - 22:56 
GeneralRe: Don't work for me :-(memberjbu208 Sep '08 - 9:16 
GeneralRe: Here is a much simplier solution...memberEd The Fish16 Nov '08 - 6:35 
GeneralRe: Here is a much simplier solution...memberSupermanDT17 Nov '08 - 2:16 
GeneralRe: Here is a much simplier solution...memberXX sdfsbh12 Apr '10 - 3:46 
GeneralSee this control as well.memberTittle Joseph20 Sep '06 - 22:53 
GeneralGood one!memberargatxa5 Sep '06 - 8:44 
QuestionPlease help mememberleo237 Jun '06 - 14:07 
GeneralTypo in codemembercode_project_user19 Apr '06 - 5:13 
GeneralRe: Typo in codememberchris lasater19 Apr '06 - 5:40 
GeneralRe: Typo in codemembercode_project_user19 Apr '06 - 8:48 
GeneralNice but uselessmemberF1Nas16 Jun '05 - 3:05 
GeneralRe: Nice but uselessmemberpajacobs18 Jul '05 - 7:55 
GeneralNot useless, MS Prefferedmemberchris lasater29 Mar '06 - 6:08 
GeneralRe: Not useless, MS PrefferedmemberLokheed20 Jun '06 - 10:19 
Generalhelp me im newmembermohammed198315 Jun '05 - 3:12 
Generalhttp://aspzone.com/articles/207.aspxsussAnonymous12 Jun '05 - 0:28 
GeneralSeems it does NOT work when verification is requiredmemberzlei1210 Jun '05 - 17:57 
GeneralRe: Seems it does NOT work when verification is requiredmemberchris lasater13 Jun '05 - 4:23 
GeneralNice trick!sussEugene Rosenfeld10 Jun '05 - 4:46 
GeneralRe: Nice trick!memberchris lasater10 Jun '05 - 4:49 

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

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