Click here to Skip to main content
12,074,763 members (58,868 online)
Click here to Skip to main content
Add your own
alternative version

Stats

154K views
673 downloads
50 bookmarked
Posted

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>

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


You may also be interested in...

Comments and Discussions

 
GeneralA method to encapsulate all in one Pin
eatemadi5-May-11 10:33
membereatemadi5-May-11 10:33 
GeneralPost back problem Pin
daakanksha10-Aug-09 0:17
memberdaakanksha10-Aug-09 0:17 
GeneralHere is a much simplier solution... Pin
SupermanDT31-Jul-07 2:26
memberSupermanDT31-Jul-07 2:26 
GeneralRe: Here is a much simplier solution... Pin
stormcandi22-Aug-07 9:57
memberstormcandi22-Aug-07 9:57 
GeneralRe: Here is a much simplier solution... Pin
johram4-Sep-08 23:56
memberjohram4-Sep-08 23:56 
GeneralRe: Don't work for me :-( Pin
jbu208-Sep-08 10:16
memberjbu208-Sep-08 10:16 
GeneralRe: Here is a much simplier solution... Pin
Ed The Fish16-Nov-08 7:35
memberEd The Fish16-Nov-08 7:35 
GeneralRe: Here is a much simplier solution... Pin
SupermanDT17-Nov-08 3:16
memberSupermanDT17-Nov-08 3:16 
GeneralRe: Here is a much simplier solution... Pin
XX sdfsbh12-Apr-10 4:46
memberXX sdfsbh12-Apr-10 4:46 
GeneralSee this control as well. Pin
Tittle Joseph20-Sep-06 23:53
memberTittle Joseph20-Sep-06 23:53 
GeneralGood one! Pin
argatxa5-Sep-06 9:44
memberargatxa5-Sep-06 9:44 
QuestionPlease help me Pin
leo237-Jun-06 15:07
memberleo237-Jun-06 15:07 
GeneralTypo in code Pin
code_project_user19-Apr-06 6:13
membercode_project_user19-Apr-06 6:13 
GeneralRe: Typo in code Pin
chris lasater19-Apr-06 6:40
memberchris lasater19-Apr-06 6:40 
GeneralRe: Typo in code Pin
code_project_user19-Apr-06 9:48
membercode_project_user19-Apr-06 9:48 
GeneralNice but useless Pin
F1Nas16-Jun-05 4:05
memberF1Nas16-Jun-05 4:05 
GeneralRe: Nice but useless Pin
pajacobs18-Jul-05 8:55
memberpajacobs18-Jul-05 8:55 
GeneralNot useless, MS Preffered Pin
chris lasater29-Mar-06 7:08
memberchris lasater29-Mar-06 7:08 
GeneralRe: Not useless, MS Preffered Pin
Lokheed20-Jun-06 11:19
memberLokheed20-Jun-06 11:19 
Generalhelp me im new Pin
mohammed198315-Jun-05 4:12
membermohammed198315-Jun-05 4:12 
Generalhttp://aspzone.com/articles/207.aspx Pin
Anonymous12-Jun-05 1:28
sussAnonymous12-Jun-05 1:28 
GeneralSeems it does NOT work when verification is required Pin
zlei1210-Jun-05 18:57
memberzlei1210-Jun-05 18:57 
GeneralRe: Seems it does NOT work when verification is required Pin
chris lasater13-Jun-05 5:23
memberchris lasater13-Jun-05 5:23 
GeneralNice trick! Pin
Eugene Rosenfeld10-Jun-05 5:46
sussEugene Rosenfeld10-Jun-05 5:46 
GeneralRe: Nice trick! Pin
chris lasater10-Jun-05 5:49
memberchris lasater10-Jun-05 5: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.

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