Disable a button on click to prevent multiple postbacks

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sangam Uprety
    New Member
    • Jun 2011
    • 4

    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>
    Last edited by Frinavale; Sep 29 '11, 03:55 PM. Reason: Link removed. If you wish to share your insights with us using this forum please post the insight here. This volunteer based forum is designed to help people solve their problems and your help is g
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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.
    Last edited by Frinavale; Sep 30 '11, 03:37 PM.

    Comment

    • Sangam Uprety
      New Member
      • Jun 2011
      • 4

      #3
      Oh then we have better know it. Could you illustrate it please? Thanks.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Well, put a button on the page.
        Add some JavaScript that will disable the button:
        Code:
        <script type="text/javascript">
            function Disable(x) {
                x.disabled = true;
            }
        </script>
        Set the button's UseSubmitBehavi or Property to false and set the button's OnClientClick Property to the JavaScript method that disables the control.

        Like this:
        Code:
        <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:

        Code:
        <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 UseSubmitBehavi or property to false or else your button will get disabled and it will not submit to the server.

        -Frinny
        Last edited by Frinavale; Sep 30 '11, 02:41 PM.

        Comment

        • MajDuke30
          New Member
          • Mar 2012
          • 1

          #5
          Thank you

          Thank you SO MUCH for your code snippets, Frinavale! They saved me a TON of time and were exactly what I was looking for!

          Originally posted by Frinavale
          Well, put a button on the page.
          Add some JavaScript that will disable the button:
          Code:
          <script type="text/javascript">
              function Disable(x) {
                  x.disabled = true;
              }
          </script>
          Set the button's UseSubmitBehavi or Property to false and set the button's OnClientClick Property to the JavaScript method that disables the control.

          Like this:
          Code:
          <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:

          Code:
          <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 UseSubmitBehavi or property to false or else your button will get disabled and it will not submit to the server.

          -Frinny

          Comment

          • Srikanth7989
            New Member
            • Apr 2021
            • 1

            #6
            It's working fine for disable but after process is done how can we re enable it

            Comment

            Working...