468,294 Members | 1,907 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,294 developers. It's quick & easy.

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 25430
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
2 Weeks Ago #6

Post your reply

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

Similar topics

1 post views Thread by Karuppasamy | last post: by
8 posts views Thread by Lee | last post: by
5 posts views Thread by Jon Booth | last post: by
4 posts views Thread by Lenny Shprekher | last post: by
3 posts views Thread by Tina | last post: by
1 post views Thread by Mardy | last post: by
4 posts views Thread by Buddy Ackerman | last post: by
4 posts views Thread by Aleks Kleyn | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.