Preventing second click of a button

C

chrisp

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)
 
S

sloan

You can fish through this for some ideas:

YouHaveToFigureOut
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_CLICKED_VARIABLE_NAME =
"hrefAlreadyClicked";
private static readonly string
HREF_ALREADY_CLICKED_VARIABLE_NAME_JS_KEY =
"HREF_ALREADY_CLICKED_VARIABLE_NAME_JS_KEY";
private static readonly string JS_INDENT = " ";




/// <summary>
/// Provides the double submit prevention on controls issuing a
PostBack.
/// </summary>
/// <param name="TargetPage">The target page.</param>
/// <param name="c">The control.</param>
public static void DoubleSubmitPrevention(Page TargetPage,
System.Web.UI.WebControls.WebControl c)
{
DoubleSubmitPrevention(TargetPage, c, string.Empty);
}


/// <summary>
/// Doubles the submit prevention.
/// </summary>
/// <param name="TargetPage">The target page.</param>
/// <param name="c">The control which needs double submit
prevention. Button, LinkButton, or ImageButton.</param>
/// <param name="submitImageName">Name of the submit image.</param>
public static void DoubleSubmitPrevention(Page TargetPage,
System.Web.UI.WebControls.WebControl c, string submitImageName)
{
DoubleSubmitPrevention(TargetPage, 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="TargetPage">The target page.</param>
/// <param name="c">The control which needs double submit
prevention. Button, LinkButton, or ImageButton.</param>
/// <param name="submitImageName">Name of the alternate image to
show while the PostBack is occuring.</param>
/// <param name="imageDelayMilliseconds">The image delay
milliseconds. Suggested value is around 125.</param>
public static void DoubleSubmitPrevention(Page TargetPage,
System.Web.UI.WebControls.WebControl c, string submitImageName, int
imageDelayMilliseconds)
{

string wcUID = c.ID;


// We need a member variable to track this.......so register it
here
YouHaveToFigureOutRegisterGenericJavaScriptBlock(TargetPage,
"var " + HREF_ALREADY_CLICKED_VARIABLE_NAME + "=false;",
HREF_ALREADY_CLICKED_VARIABLE_NAME_JS_KEY, true);

string pleaseWait = "Please Wait...";

System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (TargetPage.Validators.Count > 0)
{
sb.Append("if (typeof(Page_ClientValidate) == 'function')
{ ");
sb.Append("if (Page_ClientValidate() == false) { return
false; }} ");
}
if ((c is System.Web.UI.WebControls.Button))
{
sb.Append("this.value = '" + pleaseWait + "';");
}
else if ((c is System.Web.UI.WebControls.LinkButton))
{
sb.Append("this.innerHTML = '" + pleaseWait +
"';if(hrefAlreadyClicked==false){" + HREF_ALREADY_CLICKED_VARIABLE_NAME +
"=true;return true;}else{this.innerHTML+='...';return false;};");
}
else if ((c is System.Web.UI.WebControls.ImageButton))
{
YouHaveToFigureOutRegisterGenericJavaScriptBlock(TargetPage,
"var imgSaveButtonAlternate = new Image().src = '" + submitImageName + "'",
"ImagePreLoad", true);
sb.Append("this.src = '" + submitImageName + "';");
sb.Append("setTimeout('" +
TargetPage.ClientScript.GetPostBackEventReference(c , null).Replace("'",
"\\'") + ";', " + imageDelayMilliseconds + ");");
}
else
{
throw new ArgumentException("This procedure only accepts '
System.Web.UI.WebControls.Button', 'System.Web.UI.WebControls.LinkButton' ,
and ' System.Web.UI.WebControls.ImageButton' objects");
}


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


if (!((c is System.Web.UI.WebControls.ImageButton)))
{
sb.Append(TargetPage.ClientScript.GetPostBackEventReference(c,
null));
}
sb.Append(";");


YouHaveToFigureOutAppendAttribute(c, "onClick", sb.ToString());


sb = null;
}
 
B

bruce barker

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top