472,988 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 software developers and data experts.

Disable a button on click to prevent multiple postbacks

Most of the times we wish to disable the asp.net button control just after the first click so that we may prevent multiple postbacks. This is sensitive work since multiple posting may cause multiple transactions and bring about problems. So we need a proper solution.
One solution would be to catch the click event from client side scripting and immediately disable the button. But this approach has the problem that the server side event won't fire. Then what? So I have a bit different approach.
Catch the click event from client side using javascript or jquery, hide the button, show some proper message and let the server side event fire and get executed. After postback you will get your button as it is. Thanks.

<link removed>
Sep 29 '11 #1
5 29247
Frinavale
9,735 Expert Mod 8TB
There's a way to do it so that the server side event is executed.
You just need to know how to disable the button so that when it is disabled the page will still submit to the server :)

You just need to configure your button properly.
Sep 29 '11 #2
Oh then we have better know it. Could you illustrate it please? Thanks.
Sep 30 '11 #3
Frinavale
9,735 Expert Mod 8TB
Well, put a button on the page.
Add some JavaScript that will disable the button:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function Disable(x) {
  3.         x.disabled = true;
  4.     }
  5. </script>
Set the button's UseSubmitBehavior Property to false and set the button's OnClientClick Property to the JavaScript method that disables the control.

Like this:
Expand|Select|Wrap|Line Numbers
  1. <asp:Button ID="doIt" runat="server" Text="Do It" OnClientClick="Disable(this);" UseSubmitBehavior="false" />
You could shorten this down even further and skip the JavaScript function completely...

Just disable the button using JavaScript right in the OnClientClick...like this:

Expand|Select|Wrap|Line Numbers
  1. <asp:Button ID="doIt" runat="server" Text="Do It" OnClientClick="this.disabled=true;" UseSubmitBehavior="false" />
Tada!

The important part is that you set the button's UseSubmitBehavior property to false or else your button will get disabled and it will not submit to the server.

-Frinny
Sep 30 '11 #4
Thank you SO MUCH for your code snippets, Frinavale! They saved me a TON of time and were exactly what I was looking for!

@Frinavale
Mar 8 '12 #5
Srikanth7989
1 Bit
It's working fine for disable but after process is done how can we re enable it
Apr 28 '21 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Karuppasamy | last post by:
Hi In Windows Application (C#), on RightMouse Button click in a Textbox, a Popup menu with Cut / Copy / Paste is appearing. How to disable this behaviour. I dont want to allow the user to copy...
1
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest project has me kinda stumped and after a couple...
8
by: Lee | last post by:
Hi,I'm trying to have a button that fires a click event only once. What is the best way to do so that after the first button click, any the following clicks will be ignored.
4
by: Diane Selby | last post by:
Hi- I am developing an ASP.NET application that can take a few seconds to process the request from the user. We are looking for a client-side solution that will prevent users from resubmitting...
5
by: Jon Booth | last post by:
Hi All, I'm trying to create a button that once clicked will disable itself (so as not to be clicked twice) I have the following in my cs Button1.Attributes.Add("onClick","return...
4
by: Lenny Shprekher | last post by:
I am using regular server site ASP.NET button to execute function. Works fine, issue is that when I call page-refresh on IE after click I done it is calling same event again. Could anybody suggest...
3
by: Tina | last post by:
I have an aspx page with a textbox and an "add to my Account Button." when the button is pushed, my program adds the amount in the textbox to the database which, lets say takes a few seconds. If...
1
by: Mardy | last post by:
I have an application where, in a few cases, I can hear more than one click by the browser on a post back. I also see the IE progress bar appear twice consecutively. This happens for example when I...
4
by: Buddy Ackerman | last post by:
If I put client side code to disable a button (so that a user doesn't click it twice and double post the form) then teh button doesn't get posted with the form and the button's server-side click...
4
by: Aleks Kleyn | last post by:
In asp.net I can put code for button click event. However I come to this event twice. What is the reason and how to prevent or check second entry to this event. I need to make sure that i do not...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.