Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

Disable submit button once it is clicked.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
zlf - 28 Dec 2007 12:28 GMT
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
sloan - 28 Dec 2007 13:34 GMT
You can try this code:

MyCompany.Web.Utilities.PageUtilityLib.RegisterGenericJavaScriptBlock 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";

       /// <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.RegisterGenericJavaScriptBlock(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.RegisterGenericJavaScriptBlock(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;
       }
Mark Rae [MVP] - 28 Dec 2007 13:46 GMT
> 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;" />

Signature

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

clintonG - 28 Dec 2007 18:32 GMT
Clickety-Click Clickety-Click
Mark's solution was really slick.

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

>> 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;" />
zlf - 02 Feb 2008 08:06 GMT
Thanks!
I have another question. How to re-enable it once an exception occurs?
Now, if exception occurs, a message box pops up and the button remains
disabled. So I cannot click that button till I refresh that page manually.

"Mark Rae [MVP]" <mark@markNOSPAMrae.net>
??????:OBFUJgVSIHA.4684@TK2MSFTNGP06.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.
>
> <asp:Button ID="cmdSubmit" runat="server" Text="Submit"
> OnClick="cmdSubmit_Click" OnClientClick="this.disabled=true;" />
Mark Rae [MVP] - 02 Feb 2008 10:11 GMT
>>> 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,
[quoted text clipped - 6 lines]
> Now, if exception occurs, a message box pops up and the button remains
> disabled. So I cannot click that button till I refresh that page manually.

Where does the error occur? What causes the message to pop up?

Signature

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

zlf - 03 Feb 2008 13:10 GMT
asp:UpdatePanel  pops exceptions up.

I tried the script below, but it fails "Object expected" after 5 secs I
click the button. Can you tell me what is wrong with my script?

SelectAllButton.Attributes["onclick"] =
GetPostBackEventReference(SelectAllButton) +
               ";getElementById('SelectAllButton').disabled=true;setTimeout(\"getElementById('SelectAllButton').disabled=false\",5000);";

Thanks

"Mark Rae [MVP]" <mark@markNOSPAMrae.net>
??????:%23ql9wPYZIHA.208@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
[quoted text clipped - 9 lines]
>
> Where does the error occur? What causes the message to pop up?
zlf - 03 Feb 2008 13:13 GMT
I forgot to prefix document before getElementById. It works now after
prefixing document.

SelectAllButton.Attributes["onclick"] =
GetPostBackEventReference(SelectAllButton) +
               ";getElementById('SelectAllButton').disabled=true;setTimeout(\"document.getElementById('SelectAllButton').disabled=false;\",5000);";

Thanks

zlf

"zlf" <zlfcn@hotmail.com> дÈëÏûÏ¢ÐÂÎÅ:OBIQxYmZIHA.1212@TK2MSFTNGP05.phx.gbl...
> asp:UpdatePanel  pops exceptions up.
>
[quoted text clipped - 24 lines]
>>
>> Where does the error occur? What causes the message to pop up?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.