Ticket Q438771
Visible to All Users

ASPxButton - How to prevent a button double click when a postback is sent

created 12 years ago

I am trying to stop users from double clicking my ASPxButton.
What's happening is they click the button twice, and two post backs occur and two entries get created.

I want to disable the button after click to try prevent the problem (i realise this is not full proof, but good enough)
I managed to do this using the SendPostBack and SetEnabled on the click event.
This however breaks if the user hits enter.
The page reloads, but the button click event is not fired.

My button looks like this

ASPx
<dx:ASPxButton ID="btn" OnClick="btn_Click" runat="server" Text="ASPxButton"> <ClientSideEvents Click="function(s,e){ s.SendPostBack('Click'); s.SetEnabled(false); }"></ClientSideEvents> </dx:ASPxButton>

My Code Behind like this

C#
protected void Page_Load(object sender, EventArgs e) { Thread.Sleep(1000); if(IsPostBack) { lbl.Text = DateTime.Now + " - Page Load Called<br/>"; } } protected void btn_Click(object sender, EventArgs e) { lbl.Text += DateTime.Now + " - Button Click Called<br/>"; }

If you click on the button with your mouse, both server side events are fired.
If you hit enter on the keyboard (even when the button is selected), btn_Click never gets fired.

Please can you help me to fix this issue so the enter key will work and disable the button
Attached is my small example of the problem

Answers approved by DevExpress Support

created 12 years ago (modified 11 years ago)

Hello Grant,
I suggest you implement the following solution to this issue:

ASPx
<dx:ASPxButton ID="btn" OnClick="btn_Click" runat="server" Text="ASPxButton"> <ClientSideEvents Click="OnClick"></ClientSideEvents> </dx:ASPxButton>
JavaScript
var isPostbackInitiated = false; function OnClick(s, e) { e.processOnServer = !isPostbackInitiated; isPostbackInitiated = true; }

I have created a sample project based on the provided page. You can find it in the attachment.

    Comments (3)

      This is a great solution, thank you Vlad.
      Would it be possible to disable the button without stopping the post back ?

      DevExpress Support Team 12 years ago

        I am glad to hear that my assistance was helpful.
        >Would it be possible to disable the button without stopping the post back ?
        I have found the ASPxButton send postbacks even if they are disabled ticket where a similar task was discussed. Please refer to it for details.
        Once your question has been resolved to your satisfaction, mark a corresponding Answer as a Solution. Your question status will immediately become Closed.
        SC2 FAQ

          I know this is old, but I just came across a bug regarding this. I took over some code where the developer must have found this solution (code exact verbatim).
          Anyway, just a note, this solution will prevent ANY postback/callback from happening if validation fails on the first click. I'd suggest checking for validation to pass, if used of course, prior to performing the logic suggested.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.