HighTechTalks DotNet Forums  

Disable submit button once it is clicked.

ASP.net ASP.net discussions (microsoft.public.dotnet.framework.aspnet)


Discuss Disable submit button once it is clicked. in the ASP.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
zlf
 
Posts: n/a

Default Disable submit button once it is clicked. - 12-28-2007 , 07:28 AM






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



Reply With Quote
  #2  
Old   
sloan
 
Posts: n/a

Default Re: Disable submit button once it is clicked. - 12-28-2007 , 08:34 AM






You can try this code:

MyCompany.Web.Utilities.PageUtilityLib.RegisterGen ericJavaScriptBlock is
just a wrapper for registering a client side java script.

MyCompany.Web.Utilities.ObjectUtilityLib.AppendAtt ribute 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";

/// <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
MyCompany.Web.Utilities.PageUtilityLib.RegisterGen ericJavaScriptBlock(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.RegisterGen ericJavaScriptBlock(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.GetPostBackEvent Reference(c,
null));
}
sb.Append(";");
MyCompany.Web.Utilities.ObjectUtilityLib.AppendAtt ribute(c,
"onClick", sb.ToString(), Web.Utilities.AppendOrder.AFTER_CURRENT_ITEMS);
sb = null;
}



Reply With Quote
  #3  
Old   
Mark Rae [MVP]
 
Posts: n/a

Default Re: Disable submit button once it is clicked. - 12-28-2007 , 08:46 AM



"zlf" <zlfcn (AT) hotmail (DOT) com> wrote


Quote:
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.
<asp:Button ID="cmdSubmit" runat="server" Text="Submit"
OnClick="cmdSubmit_Click" OnClientClick="this.disabled=true;" />


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



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.