Preventing second click of a button

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chrisp

    #1

    Preventing second click of a button

    I have an ASP.NET 2 page with a button that causes a credit card transaction
    to be authorised. The authorisation procedure may take a few seconds and so
    I want to prevent the user from clicking the button again (or at least
    detect that an authorisation is already in progress and do nothing) while
    the first authorisation is in progress. Can someone help me out?

    I've tried the following but none of the solutions work:
    1) Disabling the button via JavaScript when it is clicked. This prevents the
    server-side event from firing.
    2) Disabling the button via JavaScript when it is clicked and calling
    doPostback. This just causes an 'Object Expected' error. (I have fully
    qualified the button's Id as the first parameter passed to doPostback.)
    3) Disabling the button in the server-side Click event. The button is not
    disabled until the Click event is finished. (i.e. After the authorisation)



  • sloan

    #2
    Re: Preventing second click of a button




    You can fish through this for some ideas:

    YouHaveToFigure Out
    means that I had a helper library do to this, you'll have to infer what I
    did.
    They should be intuitive.




    private static readonly string HREF_ALREADY_CL ICKED_VARIABLE_ NAME =
    "hrefAlreadyCli cked";
    private static readonly string
    HREF_ALREADY_CL ICKED_VARIABLE_ NAME_JS_KEY =
    "HREF_ALREADY_C LICKED_VARIABLE _NAME_JS_KEY";
    private static readonly string JS_INDENT = " ";




    /// <summary>
    /// Provides the double submit prevention on controls issuing a
    PostBack.
    /// </summary>
    /// <param name="TargetPag e">The target page.</param>
    /// <param name="c">The control.</param>
    public static void DoubleSubmitPre vention(Page TargetPage,
    System.Web.UI.W ebControls.WebC ontrol c)
    {
    DoubleSubmitPre vention(TargetP age, c, string.Empty);
    }


    /// <summary>
    /// Doubles the submit prevention.
    /// </summary>
    /// <param name="TargetPag e">The target page.</param>
    /// <param name="c">The control which needs double submit
    prevention. Button, LinkButton, or ImageButton.</param>
    /// <param name="submitIma geName">Name of the submit image.</param>
    public static void DoubleSubmitPre vention(Page TargetPage,
    System.Web.UI.W ebControls.WebC ontrol c, string submitImageName )
    {
    DoubleSubmitPre vention(TargetP age, c, submitImageName , 125);// a
    125 milliseconds delay seems to be a good balance
    }


    /// <summary>
    /// Provides the double submit prevention on controls issuing a
    PostBack.
    /// </summary>
    /// <param name="TargetPag e">The target page.</param>
    /// <param name="c">The control which needs double submit
    prevention. Button, LinkButton, or ImageButton.</param>
    /// <param name="submitIma geName">Name of the alternate image to
    show while the PostBack is occuring.</param>
    /// <param name="imageDela yMilliseconds"> The image delay
    milliseconds. Suggested value is around 125.</param>
    public static void DoubleSubmitPre vention(Page TargetPage,
    System.Web.UI.W ebControls.WebC ontrol c, string submitImageName , int
    imageDelayMilli seconds)
    {

    string wcUID = c.ID;


    // We need a member variable to track this.......so register it
    here
    YouHaveToFigure OutRegisterGene ricJavaScriptBl ock(TargetPage,
    "var " + HREF_ALREADY_CL ICKED_VARIABLE_ NAME + "=false;",
    HREF_ALREADY_CL ICKED_VARIABLE_ NAME_JS_KEY, true);

    string pleaseWait = "Please Wait...";

    System.Text.Str ingBuilder sb = new System.Text.Str ingBuilder();
    if (TargetPage.Val idators.Count 0)
    {
    sb.Append("if (typeof(Page_Cl ientValidate) == 'function')
    { ");
    sb.Append("if (Page_ClientVal idate() == false) { return
    false; }} ");
    }
    if ((c is System.Web.UI.W ebControls.Butt on))
    {
    sb.Append("this .value = '" + pleaseWait + "';");
    }
    else if ((c is System.Web.UI.W ebControls.Link Button))
    {
    sb.Append("this .innerHTML = '" + pleaseWait +
    "';if(hrefAlrea dyClicked==fals e){" + HREF_ALREADY_CL ICKED_VARIABLE_ NAME +
    "=true;retu rn true;}else{this .innerHTML+='.. .';return false;};");
    }
    else if ((c is System.Web.UI.W ebControls.Imag eButton))
    {
    YouHaveToFigure OutRegisterGene ricJavaScriptBl ock(TargetPage,
    "var imgSaveButtonAl ternate = new Image().src = '" + submitImageName + "'",
    "ImagePreLo ad", true);
    sb.Append("this .src = '" + submitImageName + "';");
    sb.Append("setT imeout('" +
    TargetPage.Clie ntScript.GetPos tBackEventRefer ence(c , null).Replace(" '",
    "\\'") + ";', " + imageDelayMilli seconds + ");");
    }
    else
    {
    throw new ArgumentExcepti on("This procedure only accepts '
    System.Web.UI.W ebControls.Butt on', 'System.Web.UI. WebControls.Lin kButton' ,
    and ' System.Web.UI.W ebControls.Imag eButton' objects");
    }


    sb.Append("this .disabled=true; ");


    if (!((c is System.Web.UI.W ebControls.Imag eButton)))
    {
    sb.Append(Targe tPage.ClientScr ipt.GetPostBack EventReference( c,
    null));
    }
    sb.Append(";");


    YouHaveToFigure OutAppendAttrib ute(c, "onClick", sb.ToString());


    sb = null;
    }






    "chrisp" <chrisfellows@n ospam.co.ukwrot e in message
    news:uimL7FxKJH A.3080@TK2MSFTN GP06.phx.gbl...
    >I have an ASP.NET 2 page with a button that causes a credit card
    >transaction to be authorised. The authorisation procedure may take a few
    >seconds and so I want to prevent the user from clicking the button again
    >(or at least detect that an authorisation is already in progress and do
    >nothing) while the first authorisation is in progress. Can someone help me
    >out?
    >
    I've tried the following but none of the solutions work:
    1) Disabling the button via JavaScript when it is clicked. This prevents
    the server-side event from firing.
    2) Disabling the button via JavaScript when it is clicked and calling
    doPostback. This just causes an 'Object Expected' error. (I have fully
    qualified the button's Id as the first parameter passed to doPostback.)
    3) Disabling the button in the server-side Click event. The button is not
    disabled until the Click event is finished. (i.e. After the authorisation)
    >
    >
    >

    Comment

    • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

      #3
      Re: Preventing second click of a button

      this only handles double click only, not refresh, or refresh and click, or
      back from the next page.

      you should assign a transaction guid, and put it in a hidden field on
      render. on postback, check if the guid has been processed, if so error, else
      process and log as processed.

      -- bruce (sqlwork.com)


      "sloan" wrote:
      >
      >
      >
      You can fish through this for some ideas:
      >
      YouHaveToFigure Out
      means that I had a helper library do to this, you'll have to infer what I
      did.
      They should be intuitive.
      >
      >
      >
      >
      private static readonly string HREF_ALREADY_CL ICKED_VARIABLE_ NAME =
      "hrefAlreadyCli cked";
      private static readonly string
      HREF_ALREADY_CL ICKED_VARIABLE_ NAME_JS_KEY =
      "HREF_ALREADY_C LICKED_VARIABLE _NAME_JS_KEY";
      private static readonly string JS_INDENT = " ";
      >
      >
      >
      >
      /// <summary>
      /// Provides the double submit prevention on controls issuing a
      PostBack.
      /// </summary>
      /// <param name="TargetPag e">The target page.</param>
      /// <param name="c">The control.</param>
      public static void DoubleSubmitPre vention(Page TargetPage,
      System.Web.UI.W ebControls.WebC ontrol c)
      {
      DoubleSubmitPre vention(TargetP age, c, string.Empty);
      }
      >
      >
      /// <summary>
      /// Doubles the submit prevention.
      /// </summary>
      /// <param name="TargetPag e">The target page.</param>
      /// <param name="c">The control which needs double submit
      prevention. Button, LinkButton, or ImageButton.</param>
      /// <param name="submitIma geName">Name of the submit image.</param>
      public static void DoubleSubmitPre vention(Page TargetPage,
      System.Web.UI.W ebControls.WebC ontrol c, string submitImageName )
      {
      DoubleSubmitPre vention(TargetP age, c, submitImageName , 125);// a
      125 milliseconds delay seems to be a good balance
      }
      >
      >
      /// <summary>
      /// Provides the double submit prevention on controls issuing a
      PostBack.
      /// </summary>
      /// <param name="TargetPag e">The target page.</param>
      /// <param name="c">The control which needs double submit
      prevention. Button, LinkButton, or ImageButton.</param>
      /// <param name="submitIma geName">Name of the alternate image to
      show while the PostBack is occuring.</param>
      /// <param name="imageDela yMilliseconds"> The image delay
      milliseconds. Suggested value is around 125.</param>
      public static void DoubleSubmitPre vention(Page TargetPage,
      System.Web.UI.W ebControls.WebC ontrol c, string submitImageName , int
      imageDelayMilli seconds)
      {
      >
      string wcUID = c.ID;
      >
      >
      // We need a member variable to track this.......so register it
      here
      YouHaveToFigure OutRegisterGene ricJavaScriptBl ock(TargetPage,
      "var " + HREF_ALREADY_CL ICKED_VARIABLE_ NAME + "=false;",
      HREF_ALREADY_CL ICKED_VARIABLE_ NAME_JS_KEY, true);
      >
      string pleaseWait = "Please Wait...";
      >
      System.Text.Str ingBuilder sb = new System.Text.Str ingBuilder();
      if (TargetPage.Val idators.Count 0)
      {
      sb.Append("if (typeof(Page_Cl ientValidate) == 'function')
      { ");
      sb.Append("if (Page_ClientVal idate() == false) { return
      false; }} ");
      }
      if ((c is System.Web.UI.W ebControls.Butt on))
      {
      sb.Append("this .value = '" + pleaseWait + "';");
      }
      else if ((c is System.Web.UI.W ebControls.Link Button))
      {
      sb.Append("this .innerHTML = '" + pleaseWait +
      "';if(hrefAlrea dyClicked==fals e){" + HREF_ALREADY_CL ICKED_VARIABLE_ NAME +
      "=true;retu rn true;}else{this .innerHTML+='.. .';return false;};");
      }
      else if ((c is System.Web.UI.W ebControls.Imag eButton))
      {
      YouHaveToFigure OutRegisterGene ricJavaScriptBl ock(TargetPage,
      "var imgSaveButtonAl ternate = new Image().src = '" + submitImageName + "'",
      "ImagePreLo ad", true);
      sb.Append("this .src = '" + submitImageName + "';");
      sb.Append("setT imeout('" +
      TargetPage.Clie ntScript.GetPos tBackEventRefer ence(c , null).Replace(" '",
      "\\'") + ";', " + imageDelayMilli seconds + ");");
      }
      else
      {
      throw new ArgumentExcepti on("This procedure only accepts '
      System.Web.UI.W ebControls.Butt on', 'System.Web.UI. WebControls.Lin kButton' ,
      and ' System.Web.UI.W ebControls.Imag eButton' objects");
      }
      >
      >
      sb.Append("this .disabled=true; ");
      >
      >
      if (!((c is System.Web.UI.W ebControls.Imag eButton)))
      {
      sb.Append(Targe tPage.ClientScr ipt.GetPostBack EventReference( c,
      null));
      }
      sb.Append(";");
      >
      >
      YouHaveToFigure OutAppendAttrib ute(c, "onClick", sb.ToString());
      >
      >
      sb = null;
      }
      >
      >
      >
      >
      >
      >
      "chrisp" <chrisfellows@n ospam.co.ukwrot e in message
      news:uimL7FxKJH A.3080@TK2MSFTN GP06.phx.gbl...
      I have an ASP.NET 2 page with a button that causes a credit card
      transaction to be authorised. The authorisation procedure may take a few
      seconds and so I want to prevent the user from clicking the button again
      (or at least detect that an authorisation is already in progress and do
      nothing) while the first authorisation is in progress. Can someone help me
      out?

      I've tried the following but none of the solutions work:
      1) Disabling the button via JavaScript when it is clicked. This prevents
      the server-side event from firing.
      2) Disabling the button via JavaScript when it is clicked and calling
      doPostback. This just causes an 'Object Expected' error. (I have fully
      qualified the button's Id as the first parameter passed to doPostback.)
      3) Disabling the button in the server-side Click event. The button is not
      disabled until the Click event is finished. (i.e. After the authorisation)

      >
      >
      >

      Comment

      Working...