Click here to Skip to main content
15,498,506 members
Articles / Web Development / ASP.NET
Posted 9 Jun 2005


54 bookmarked

The amazing ASP.NET "One Time Only Clickable" Button

Rate me:
Please Sign up or sign in to vote.
3.43/5 (31 votes)
9 Jun 20051 min read
A button which gets disabled during postback and can therefore only be clicked once.

Picture of the Button Control


There often comes the time when you want to prevent your users from hitting a submit button on your page twice (or whatever times they think is appropriate). As we all know there unfortunately is no "built-in" functionality to archive this behavior.


The idea is basically quite simple. When somebody clicks a button (we will use an <asp:button /> for the following example), a client side JavaScript function is called which disables the button and submits the page. After the page gets submitted (Page.IsPostBack = True), the server side method assigned to the button should fire and persist the user's input. Unfortunately it’s not that easy. If the button is disabled before the page gets submitted, the server side method assigned to the button does not fire. This is because the “Name/Value pair” of the button is not incorporated in the “Form Collection” of the page if the button is disabled. The trick is to disable the button after the page gets submitted and here is how it’s done.

Using the code

This is how the code in the .aspx page looks like. To allow you to simply copy & paste the code, I included everything within <HTML />.

        <title>One Time Clickable Button</title>
        <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" 
        <script language="javascript">

            function DisableButton() {
                window.setTimeout("disableButton('" + 
          + "')", 0);

            function disableButton(buttonID) {

        <form id="Form1" method="post" runat="server">
            <asp:Button ID="btnSave" Text="Save" Runat="server"></asp:Button>

The same applies to the code-behind file. The purpose of System.Threading.Thread.Sleep(5000) is simply to emulate a long running save operation, calculation, etc.

Private Sub Page_Load(ByVal sender As System.Object, _
               ByVal e As System.EventArgs) Handles MyBase.Load
    btnSave.Attributes.Add("onclick", "DisableButton()")
End Sub

Private Sub btnSave_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles btnSave.Click
    Response.Write("Page has been saved.")
End Sub

Have fun!


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
Program Manager Microsoft
United States United States
Program Manager Visual Studio

Comments and Discussions

Questionhow to do this in C# Pin
telme12-Oct-11 2:12
Membertelme12-Oct-11 2:12 
QuestionEasier way and no javascript require - use postback reference Pin
MTRIG12-Jul-11 4:13
MemberMTRIG12-Jul-11 4:13 
GeneralRe: Easier way and no javascript require - use postback reference Pin
stevefortner6-Sep-11 10:21
Memberstevefortner6-Sep-11 10:21 
Generalworks great! Pin
Steve Gagliardo2-Feb-11 5:19
MemberSteve Gagliardo2-Feb-11 5:19 
GeneralMy vote of 5 Pin
EricFaust7-Sep-10 6:08
MemberEricFaust7-Sep-10 6:08 
GeneralA solution that works with both IE and Firefox Pin
Tareman27-Feb-09 7:39
MemberTareman27-Feb-09 7:39 
GeneralRe: A solution that works with both IE and Firefox Pin
thohan9914-Apr-09 14:27
Memberthohan9914-Apr-09 14:27 
GeneralNot really compatible with ASP.NET Pin
johram4-Sep-08 23:28
Memberjohram4-Sep-08 23:28 
GeneralIt dosent work on fire fox - please help Pin
lavanyaashna047-Jun-08 9:21
Memberlavanyaashna047-Jun-08 9:21 
GeneralRe: It dosent work on fire fox - please help Pin
thohan9914-Apr-09 14:15
Memberthohan9914-Apr-09 14:15 
GeneralRe: It dosent work on fire fox - please help Pin
DavidLieu29-Apr-10 12:49
MemberDavidLieu29-Apr-10 12:49 
GeneralThanks for the code Pin
jamezu20-May-08 16:42
Memberjamezu20-May-08 16:42 
QuestionBrowser problem Pin
EbyZ_Ideas11-Sep-07 21:41
MemberEbyZ_Ideas11-Sep-07 21:41 
GeneralTHANKS A LOT!!!!! Pin
ricparas6-Aug-07 18:04
Memberricparas6-Aug-07 18:04 
QuestionBrowser issue Pin
kvchowdary15-Apr-07 3:38
Memberkvchowdary15-Apr-07 3:38 
GeneralIncredible Pin
undercover74710-May-06 11:20
Memberundercover74710-May-06 11:20 
GeneralRe: Incredible Pin
themosk12-May-06 10:32
Memberthemosk12-May-06 10:32 
Generalexcellent article Pin
yameen29-Oct-05 20:01
Memberyameen29-Oct-05 20:01 
GeneralRe: excellent article Pin
Daniel Walzenbach11-Nov-05 4:29
MemberDaniel Walzenbach11-Nov-05 4:29 
Generalthis rocks ! Pin
Anonymous29-Sep-05 15:18
MemberAnonymous29-Sep-05 15:18 
Generaldisabling all buttons &amp; client callbacks Pin
themastuh27-Aug-05 7:16
Memberthemastuh27-Aug-05 7:16 
GeneralAnother much easier way Pin
nmg1965-Aug-05 3:33
Membernmg1965-Aug-05 3:33 
I just added the following code to the onclick event:

onclick="this.disabled=true; __doPostBack('ButtonName',''); return false;"

Often the __doPostBack function is inserted into your page by .NET. If not, put the code in manually, or add a dummy LinkButton and set it's style to be "display:none".

No server side C# required... Smile | :)

This trick is also useful if you want to fake a button click from javascript. Eg if the user presses enter in a text box...


onKeyPress="if (event.keyCode==13) { __doPostBack('SearchButton',''); return false; }"
GeneralAn idea... Pin
Fredrik Kalseth4-Aug-05 23:34
MemberFredrik Kalseth4-Aug-05 23:34 
GeneralMetaBuilders OneClick Pin
mikejh994-Aug-05 5:20
Membermikejh994-Aug-05 5:20 
GeneralRe: MetaBuilders OneClick Pin
knapi4-Aug-05 22:10
Memberknapi4-Aug-05 22:10 

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.