Click here to Skip to main content
15,878,852 members
Articles / Web Development / ASP.NET
Article

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 214.4K   903   54   41
A button which gets disabled during postback and can therefore only be clicked once.

Picture of the Button Control

Introduction

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.

Background

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 />.

HTML
<HTML>
    <HEAD>
        <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" 
           content="http://schemas.microsoft.com/intellisense/ie5">
        <script language="javascript">
        <!--

            function DisableButton() {
                document.forms[0].submit();
                window.setTimeout("disableButton('" + 
                   window.event.srcElement.id + "')", 0);
            }

            function disableButton(buttonID) {
                document.getElementById(buttonID).disabled=true;
            }

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

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.

VB
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
    System.Threading.Thread.Sleep(5000)
    Response.Write("Page has been saved.")
End Sub

Have fun!

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


Written By
Program Manager Microsoft
United States United States
Program Manager Visual Studio

Comments and Discussions

 
Questionhow to do this in asp.net C# Pin
telme12-Oct-11 1:12
telme12-Oct-11 1:12 
QuestionEasier way and no javascript require - use postback reference Pin
MTRIG12-Jul-11 3:13
MTRIG12-Jul-11 3:13 
GeneralRe: Easier way and no javascript require - use postback reference Pin
stevefortner6-Sep-11 9:21
stevefortner6-Sep-11 9:21 
Generalworks great! Pin
Steve Gagliardo2-Feb-11 4:19
Steve Gagliardo2-Feb-11 4:19 
GeneralMy vote of 5 Pin
EricFaust7-Sep-10 5:08
EricFaust7-Sep-10 5:08 
GeneralA solution that works with both IE and Firefox Pin
Tareman27-Feb-09 6:39
Tareman27-Feb-09 6:39 
GeneralRe: A solution that works with both IE and Firefox Pin
thohan9914-Apr-09 13:27
thohan9914-Apr-09 13:27 
GeneralNot really compatible with ASP.NET Pin
johram4-Sep-08 22:28
johram4-Sep-08 22:28 
GeneralIt dosent work on fire fox - please help Pin
lavanyaashna047-Jun-08 8:21
lavanyaashna047-Jun-08 8:21 
GeneralRe: It dosent work on fire fox - please help Pin
thohan9914-Apr-09 13:15
thohan9914-Apr-09 13:15 
GeneralRe: It dosent work on fire fox - please help Pin
DavidLieu29-Apr-10 11:49
DavidLieu29-Apr-10 11:49 
GeneralThanks for the code Pin
jamezu20-May-08 15:42
jamezu20-May-08 15:42 
QuestionBrowser problem Pin
EbyZ_Ideas11-Sep-07 20:41
EbyZ_Ideas11-Sep-07 20:41 
GeneralTHANKS A LOT!!!!! Pin
ricparas6-Aug-07 17:04
ricparas6-Aug-07 17:04 
QuestionBrowser issue Pin
kvchowdary15-Apr-07 2:38
kvchowdary15-Apr-07 2:38 
GeneralIncredible Pin
undercover74710-May-06 10:20
undercover74710-May-06 10:20 
GeneralRe: Incredible Pin
themosk12-May-06 9:32
themosk12-May-06 9:32 
Generalexcellent article Pin
yameen29-Oct-05 19:01
yameen29-Oct-05 19:01 
GeneralRe: excellent article Pin
Daniel Walzenbach11-Nov-05 3:29
Daniel Walzenbach11-Nov-05 3:29 
Generalthis rocks ! Pin
Anonymous29-Sep-05 14:18
Anonymous29-Sep-05 14:18 
Generaldisabling all buttons &amp; client callbacks Pin
themastuh27-Aug-05 6:16
themastuh27-Aug-05 6:16 
GeneralAnother much easier way Pin
nmg1965-Aug-05 2:33
nmg1965-Aug-05 2:33 
GeneralAn idea... Pin
Fredrik Kalseth4-Aug-05 22:34
Fredrik Kalseth4-Aug-05 22:34 
GeneralMetaBuilders OneClick Pin
mikejh994-Aug-05 4:20
mikejh994-Aug-05 4:20 
GeneralRe: MetaBuilders OneClick Pin
knapi4-Aug-05 21:10
knapi4-Aug-05 21: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.