Click here to Skip to main content
Licence 
First Posted 9 Jun 2005
Views 128,020
Bookmarked 50 times

Disable ASP Button on Submit and capture the PostBack OnClick Event

By | 9 Jun 2005 | Article
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);
    }
    
    /// <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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralA method to encapsulate all in one Pinmembereatemadi9:33 5 May '11  
GeneralPost back problem Pinmemberdaakanksha23:17 9 Aug '09  
GeneralHere is a much simplier solution... PinmemberSupermanDT1:26 31 Jul '07  
GeneralRe: Here is a much simplier solution... Pinmemberstormcandi8:57 22 Aug '07  
GeneralRe: Here is a much simplier solution... Pinmemberjohram22:56 4 Sep '08  
GeneralRe: Don't work for me :-( Pinmemberjbu209:16 8 Sep '08  
GeneralRe: Here is a much simplier solution... PinmemberEd The Fish6:35 16 Nov '08  
GeneralRe: Here is a much simplier solution... PinmemberSupermanDT2:16 17 Nov '08  
GeneralRe: Here is a much simplier solution... PinmemberXX sdfsbh3:46 12 Apr '10  
GeneralSee this control as well. PinmemberTittle Joseph22:53 20 Sep '06  
GeneralGood one! Pinmemberargatxa8:44 5 Sep '06  
QuestionPlease help me Pinmemberleo2314:07 7 Jun '06  
GeneralTypo in code Pinmembercode_project_user5:13 19 Apr '06  
GeneralRe: Typo in code Pinmemberchris lasater5:40 19 Apr '06  
GeneralRe: Typo in code Pinmembercode_project_user8:48 19 Apr '06  
GeneralNice but useless PinmemberF1Nas3:05 16 Jun '05  
GeneralRe: Nice but useless Pinmemberpajacobs7:55 18 Jul '05  
GeneralNot useless, MS Preffered Pinmemberchris lasater6:08 29 Mar '06  
GeneralRe: Not useless, MS Preffered PinmemberLokheed10:19 20 Jun '06  
What I discovered is that in rare circumstances (in my case a user control, no server controls with autopostback=true, and a validator), when validation fails the page does not render the __doPostBack function thus causing a javascript error when you try to re-submit the form. I worked around this by adding:
 
Page.GetPostBackClientHyperlink(submitButton, string.Empty);
 
That forces the __doPostBack function to be rendered on every page load.
Generalhelp me im new Pinmembermohammed19833:12 15 Jun '05  
Generalhttp://aspzone.com/articles/207.aspx PinsussAnonymous0:28 12 Jun '05  
GeneralSeems it does NOT work when verification is required Pinmemberzlei1217:57 10 Jun '05  
GeneralRe: Seems it does NOT work when verification is required Pinmemberchris lasater4:23 13 Jun '05  
GeneralNice trick! PinsussEugene Rosenfeld4:46 10 Jun '05  
GeneralRe: Nice trick! Pinmemberchris lasater4:49 10 Jun '05  

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.

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