Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net double-click problem

Tags:

having a slight problem with an ASP.net page of mine. If a user were to double click on a "submit" button it will write to the database twice (i.e. carry out the 'onclick' method on the imagebutton twice)

How can I make it so that if a user clicks on the imagebutton, just the imagebutton is disabled?

I've tried:

<asp:ImageButton       runat="server"       ID="VerifyStepContinue"       ImageUrl=image src       ToolTip="Go"       TabIndex="98"       CausesValidation="true"       OnClick="methodName"       OnClientClick="this.disabled = true;" /> 

But this OnClientClick property completely stops the page from being submitted! Any help?

Sorry, yes, I do have Validation controls... hence the icky problem.


Working on this still, up to this point now:

ASP code:

 <asp:TextBox ID="hidToken" runat="server" Visible="False" Enabled="False"></asp:TextBox>  ...  <asp:ImageButton runat="server" ID="InputStepContinue" Name="InputStepContinue" ImageUrl="imagesrc" ToolTip="Go" TabIndex="98" CausesValidation="true" OnClick="SubmitMethod" OnClientClick="document.getElementById('InputStepContinue').style.visibility='hidden';" /> 

C# code:

         private Random     random = new Random();       protected void Page_Load(object sender, EventArgs e)     {         //Use a Token to make sure it has only been clicked once.         if (Page.IsPostBack)         {             if (double.Parse(hidToken.Text) == ((double)Session["NextToken"]))             {                 InputMethod();             }             else             {                 // double click             }         }          double next = random.Next();          hidToken.Text = next + "";         Session["NextToken"] = next; 

Actually... this nearly works. The double click problem is pretty much fixed (yay!) The image still isn't hidden though.

like image 494
David Archer Avatar asked Sep 30 '09 13:09

David Archer


People also ask

How to prevent double click on button in mvc?

I have the following code for avoiding the double clic: jQuery. fn. preventDoubleSubmit = function () { var alreadySubmitted = false; return jQuery(this).

How do I disable double click event?

If you want to disable any mouse click action use addEventListener(event, function, useCapture) . Onclick ,call this function.


1 Answers

The general approach is twofold.

Serverside:

  1. On load of the page, generate a token (using System.Random), save it in the session, and write it to a hidden form field
  2. On submit, check that the hidden form field equals the session variable (before setting it again)
  3. Do work

Clientside:

Similar to what you have, but probably just hide the button, and replace it with some text like 'submitting'.

The important thing to note, client side, is that the user may cancel the post by hitting 'escape', so you should consider what to do here (depending on how far along they are the token won't be used, so you'll need to bring the button back from being disabled/hidden).

Complete example follows:

C# (includes code to see it in action):

<html> <head runat="server">     <title>double-click test</title>     <script language="c#" runat="server">     private Random         random = new Random();      private static int         TEST = 0;      public void Page_Load (object sender, EventArgs ea)     {         SetToken();     }      private void btnTest_Click (object sender, EventArgs ea)     {         if( IsTokenValid() ){             DoWork();         } else {             // double click             ltlResult.Text = "double click!";         }     }      private bool IsTokenValid ()     {         bool result = double.Parse(hidToken.Value) == ((double) Session["NextToken"]);         SetToken();         return result;     }      private void SetToken ()     {         double next = random.Next();          hidToken.Value       = next + "";         Session["NextToken"] = next;     }       private void DoWork ()     {         TEST++;         ltlResult.Text = "DoWork(): " + TEST + ".";     }     </script> </head> <body>     <script language="javascript">         var last = null;         function f (obj)         {             obj.src = "http://www.gravatar.com/avatar/4659883ec420f39723c3df6ed99971b9?s=32&d=identicon&r=PG";             // Note: Disabling it here produced strange results. More investigation required.             last = obj;             setTimeout("reset()", 1 * 1000);             return true;         }          function reset ()         {             last.src = "http://www.gravatar.com/avatar/495ce8981a5127a9fd24bd72e7e3664a?s=32&d=identicon&r=PG";             last.disabled = "false";         }     </script>     <form id="form1" runat="server">         <asp:HiddenField runat="server" ID="hidToken" />         <asp:ImageButton runat="server" ID="btnTest"             OnClientClick="return f(this);"             ImageUrl="http://www.gravatar.com/avatar/495ce8981a5127a9fd24bd72e7e3664a?s=32&d=identicon&r=PG" OnClick="btnTest_Click" />         <pre>Result: <asp:Literal runat="server" ID="ltlResult" /></pre>     </form> </body> </html> 
like image 189
Noon Silk Avatar answered Dec 25 '22 09:12

Noon Silk