Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to prevent multiple button clicks

How to prevent multiple button clicks

Scheduled Pinned Locked Moved C#
csharphelptutorialquestionasp-net
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MichCl
    wrote on last edited by
    #1

    I have a c# .net project that has a button on the form. When the button is clicked twice, bad things happen. How do I disable the button? I've tried disabling my button as follows but it doesn't work.

    btn_Program.Enabled = false;

    I've searched for this online, but every solution seems to reference asp.net. I don't think I have that, do I? I'm fairly new to c# and visual studio, so I may have it and not know it. I liked the following discussion, but again, it refers to asp.net. http://stackoverflow.com/questions/8473261/how-to-disable-a-button-after-1st-click[^] The example I liked had the following code in it, but when I add it to my form class, it gets a compilation problem for ClientScript and Attributes:

    static void DisableButtonDuringPostback(Page page, Button control)
    {
    StringBuilder sb = new StringBuilder();
    sb.Append("this.disabled = true;");
    sb.Append(page.ClientScript.GetPostBackEventReference(control, control.ID.ToString()));
    sb.Append(";");

    control.Attributes.Add("onclick", sb.ToString());
    

    }

    It said to add the following to Page_Load

    DisableButtonDuringPostback(this.Page, Button3);

    Compilation problems: 1. 'System.Windows.Forms.Button' does not contain a definition for id and no extension method id accepting a first argument of type 'System.Windows.Forms.Button' could be found (are you missing a using directive or an assembly reference?) 2. System.Windows.Forms.Button does not contain a definition for Attributes and no extension method Attributes accepting a first argument of type System.Windows.Forms.Button could be found (are you missing a using directive or an assembly reference?) Any help would be very much appreciated.

    L L 2 Replies Last reply
    0
    • M MichCl

      I have a c# .net project that has a button on the form. When the button is clicked twice, bad things happen. How do I disable the button? I've tried disabling my button as follows but it doesn't work.

      btn_Program.Enabled = false;

      I've searched for this online, but every solution seems to reference asp.net. I don't think I have that, do I? I'm fairly new to c# and visual studio, so I may have it and not know it. I liked the following discussion, but again, it refers to asp.net. http://stackoverflow.com/questions/8473261/how-to-disable-a-button-after-1st-click[^] The example I liked had the following code in it, but when I add it to my form class, it gets a compilation problem for ClientScript and Attributes:

      static void DisableButtonDuringPostback(Page page, Button control)
      {
      StringBuilder sb = new StringBuilder();
      sb.Append("this.disabled = true;");
      sb.Append(page.ClientScript.GetPostBackEventReference(control, control.ID.ToString()));
      sb.Append(";");

      control.Attributes.Add("onclick", sb.ToString());
      

      }

      It said to add the following to Page_Load

      DisableButtonDuringPostback(this.Page, Button3);

      Compilation problems: 1. 'System.Windows.Forms.Button' does not contain a definition for id and no extension method id accepting a first argument of type 'System.Windows.Forms.Button' could be found (are you missing a using directive or an assembly reference?) 2. System.Windows.Forms.Button does not contain a definition for Attributes and no extension method Attributes accepting a first argument of type System.Windows.Forms.Button could be found (are you missing a using directive or an assembly reference?) Any help would be very much appreciated.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Add a timestamp; if it comes in again, compare the current time to the datestamp you saved earlier. If it is <2 seconds, ignore and cancel execution of the request.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

      M 2 Replies Last reply
      0
      • M MichCl

        I have a c# .net project that has a button on the form. When the button is clicked twice, bad things happen. How do I disable the button? I've tried disabling my button as follows but it doesn't work.

        btn_Program.Enabled = false;

        I've searched for this online, but every solution seems to reference asp.net. I don't think I have that, do I? I'm fairly new to c# and visual studio, so I may have it and not know it. I liked the following discussion, but again, it refers to asp.net. http://stackoverflow.com/questions/8473261/how-to-disable-a-button-after-1st-click[^] The example I liked had the following code in it, but when I add it to my form class, it gets a compilation problem for ClientScript and Attributes:

        static void DisableButtonDuringPostback(Page page, Button control)
        {
        StringBuilder sb = new StringBuilder();
        sb.Append("this.disabled = true;");
        sb.Append(page.ClientScript.GetPostBackEventReference(control, control.ID.ToString()));
        sb.Append(";");

        control.Attributes.Add("onclick", sb.ToString());
        

        }

        It said to add the following to Page_Load

        DisableButtonDuringPostback(this.Page, Button3);

        Compilation problems: 1. 'System.Windows.Forms.Button' does not contain a definition for id and no extension method id accepting a first argument of type 'System.Windows.Forms.Button' could be found (are you missing a using directive or an assembly reference?) 2. System.Windows.Forms.Button does not contain a definition for Attributes and no extension method Attributes accepting a first argument of type System.Windows.Forms.Button could be found (are you missing a using directive or an assembly reference?) Any help would be very much appreciated.

        L Offline
        L Offline
        lukeer
        wrote on last edited by
        #3

        btn_Program.Enabled = false;

        Are you sure that btn_Program is the correct button to disable? Since you aren't sure to have started an ASP.NET project, I will assume you have a Windows Forms project instead. That would mean that you have a button click event handling routine somewhere that looks similar to this:

        void btn_Program_Click(object sender, EventArgs e)
        {
        // ... your code here ...
        }

        To definitely disable the correct button, you can use the sender parameter:

        void btn_Program_Click(object sender, EventArgs e)
        {
        // Disable the button whose Click event started this method
        System.Windows.Forms.Button methodStartingControl = sender as System.Windows.Forms.Button;
        if(methodStartingControl != null)
        {
        methodStartingControl.Enabled = false;
        }

        // ... your code here ...

        // Re-enable the previously-disabled button
        if(methodStartingControl != null)
        {
        methodStartingControl.Enabled = true;
        }
        }

        Ciao, luker

        M 1 Reply Last reply
        0
        • L lukeer

          btn_Program.Enabled = false;

          Are you sure that btn_Program is the correct button to disable? Since you aren't sure to have started an ASP.NET project, I will assume you have a Windows Forms project instead. That would mean that you have a button click event handling routine somewhere that looks similar to this:

          void btn_Program_Click(object sender, EventArgs e)
          {
          // ... your code here ...
          }

          To definitely disable the correct button, you can use the sender parameter:

          void btn_Program_Click(object sender, EventArgs e)
          {
          // Disable the button whose Click event started this method
          System.Windows.Forms.Button methodStartingControl = sender as System.Windows.Forms.Button;
          if(methodStartingControl != null)
          {
          methodStartingControl.Enabled = false;
          }

          // ... your code here ...

          // Re-enable the previously-disabled button
          if(methodStartingControl != null)
          {
          methodStartingControl.Enabled = true;
          }
          }

          Ciao, luker

          M Offline
          M Offline
          MichCl
          wrote on last edited by
          #4

          This seems to be working! Thanks!

          1 Reply Last reply
          0
          • L Lost User

            Add a timestamp; if it comes in again, compare the current time to the datestamp you saved earlier. If it is <2 seconds, ignore and cancel execution of the request.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

            M Offline
            M Offline
            MichCl
            wrote on last edited by
            #5

            Thank you! I tried the other one first, but this looks good too. For some reason I'm not seeing the ratings thingy, so I'll check back later.

            1 Reply Last reply
            0
            • L Lost User

              Add a timestamp; if it comes in again, compare the current time to the datestamp you saved earlier. If it is <2 seconds, ignore and cancel execution of the request.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

              M Offline
              M Offline
              MichCl
              wrote on last edited by
              #6

              I ran into some issues with the other idea and now I'm using your idea. Works great! Instead of using the timestamp, though, I set a boolean flag, and reset it when I'm ready for a second click.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups