Disable submit button once it is clicked.

Disable submit button once it is clicked.

am 28.12.2007 13:28:59 von zlf

How to disable button once it is clicked on the page? The codes in click
event of submit button is time-costing(about 4-5 secs), in this period, user
maybe do a re-submit, that is not expected.

Thanks

zlf

Re: Disable submit button once it is clicked.

am 28.12.2007 14:34:57 von sloan

You can try this code:

MyCompany.Web.Utilities.PageUtilityLib.RegisterGenericJavaSc riptBlock is
just a wrapper for registering a client side java script.

MyCompany.Web.Utilities.ObjectUtilityLib.AppendAttribute is just a wrapper
for myControl.Attributes.Add( .......... )


You should get the idea.

This handles 3 types of controls.






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";

///


/// Provides the double submit prevention on controls issuing a
PostBack.
///

/// The target page.
/// The control.
public static void DoubleSubmitPrevention(Page TargetPage,
System.Web.UI.WebControls.WebControl c)
{
DoubleSubmitPrevention(TargetPage, c, string.Empty);
}


///
/// Doubles the submit prevention.
///

/// The target page.
/// The control which needs double submit
prevention. Button, LinkButton, or ImageButton.
/// Name of the submit image.
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
}


///
/// Provides the double submit prevention on controls issuing a
PostBack.
///

/// The target page.
/// The control which needs double submit
prevention. Button, LinkButton, or ImageButton.
/// Name of the alternate image to
show while the PostBack is occuring.
/// The image delay
milliseconds. Suggested value is around 125.
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
MyCompany.Web.Utilities.PageUtilityLib.RegisterGenericJavaSc riptBlock(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))
{
MyCompany.Web.Utilities.PageUtilityLib.RegisterGenericJavaSc riptBlock(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(";");
MyCompany.Web.Utilities.ObjectUtilityLib.AppendAttribute(c,
"onClick", sb.ToString(), Web.Utilities.AppendOrder.AFTER_CURRENT_ITEMS);
sb = null;
}

Re: Disable submit button once it is clicked.

am 28.12.2007 14:46:43 von mark

"zlf" wrote in message
news:uYEB60USIHA.4740@TK2MSFTNGP02.phx.gbl...

> How to disable button once it is clicked on the page? The codes in click
> event of submit button is time-costing(about 4-5 secs), in this period,
> user maybe do a re-submit, that is not expected.

OnClick="cmdSubmit_Click" OnClientClick="this.disabled=true;" />


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Re: Disable submit button once it is clicked.

am 28.12.2007 19:32:23 von Patrice

Clickety-Click Clickety-Click
Mark's solution was really slick.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/


"Mark Rae [MVP]" wrote in message
news:OBFUJgVSIHA.4684@TK2MSFTNGP06.phx.gbl...
> "zlf" wrote in message
> news:uYEB60USIHA.4740@TK2MSFTNGP02.phx.gbl...
>
>> How to disable button once it is clicked on the page? The codes in click
>> event of submit button is time-costing(about 4-5 secs), in this period,
>> user maybe do a re-submit, that is not expected.
>
> > OnClick="cmdSubmit_Click" OnClientClick="this.disabled=true;" />
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net