Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent multiple submit button clicks in ASP.NET without jQuery
    primarykey
    data
    text
    <p>I am attempting to prevent multiple submits when a user clicks a link button multiple times, but do not want to use jQuery. Ideally, I would like to do this in code behind.</p> <p>My current solution is based on <a href="http://www.codeproject.com/Tips/107373/Stopping-the-multiple-postbacks-of-ASP-NET-linkbut" rel="nofollow">This Code Project Article</a> </p> <p>Currently I have this:</p> <pre><code> private const string _disableButtonScript = "return disableButton(this.id);"; /// &lt;summary&gt; /// Prevents a button being clicked multiple times on the client /// &lt;/summary&gt; /// &lt;param name="currentPage"&gt;The page the button resides on&lt;/param&gt; /// &lt;param name="button"&gt;The button to prevent the multiple clicks on&lt;/param&gt; public void PreventMultipleClientClickPostbacks(Page currentPage, LinkButton button) { if (currentPage.IsPostBack || currentPage.IsCallback) { // Already added script return; } AddDisableScriptToPageHeader(currentPage, button); string currentOnClick = string.Empty; // If onclick already exist we need to append to it if (button.Attributes["onclick"] != null) { currentOnClick = button.Attributes["onclick"]; // Remove current onclick button.Attributes.Remove("onclick"); if (!currentOnClick.EndsWith(";")) { // add semi colon suffix currentOnClick = currentOnClick + ";"; } } // add client onclick handler button.Attributes.Add("onclick", string.Format("{0}{1}", currentOnClick, _disableButtonScript)); } /// &lt;summary&gt; /// Adds the javascript disable function to the page header /// &lt;/summary&gt; /// &lt;param name="currentPage"&gt;The page to add the header script to&lt;/param&gt; /// &lt;param name="button"&gt;The button to add the script to&lt;/param&gt; private static void AddDisableScriptToPageHeader(Page currentPage, Control button) { // Generate disable script StringBuilder scriptLinkHtml = new StringBuilder(); scriptLinkHtml.AppendLine("&lt;script type=\"text/javascript\"&gt;"); scriptLinkHtml.AppendLine("//&lt;![CDATA["); scriptLinkHtml.AppendLine("var callCount = 0;"); scriptLinkHtml.AppendLine("function disableButton(btnId) {"); scriptLinkHtml.AppendLine("var ret = true;"); scriptLinkHtml.AppendLine("var ele = document.getElementById(btnId);"); scriptLinkHtml.AppendLine("if (ele != null &amp;&amp; !ele.disabled)"); scriptLinkHtml.AppendLine("ret = true;"); scriptLinkHtml.AppendLine("else"); scriptLinkHtml.AppendLine("ret = false;"); scriptLinkHtml.AppendLine("if (ele != null)"); scriptLinkHtml.AppendLine("ele.disabled = true;"); scriptLinkHtml.AppendLine("return ret;"); scriptLinkHtml.AppendLine("}"); scriptLinkHtml.AppendLine("//]]&gt;"); scriptLinkHtml.AppendLine("&lt;/script&gt;"); // Add script link to page header LiteralControl scriptLink = new LiteralControl(scriptLinkHtml.ToString()); scriptLink.ID = "disableButtonScript"; currentPage.Header.Controls.Add(scriptLink); } </code></pre> <p>This renders out on the client like this:</p> <pre><code> &lt;script type="text/javascript"&gt; function disableButton(btnId) { var ret = true; var ele = document.getElementById(btnId); if (ele != null &amp;&amp; !ele.disabled) ret = true; else ret = false; if (ele != null) ele.disabled = true; return ret; } &lt;/script&gt; &lt;a onclick="javascript:return disableButton(this.id);" id="cntMain_btnSubmit" class="but-yellow frcr" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&amp;quot;ctl00$cntMain$btnSubmit&amp;quot;, &amp;quot;&amp;quot;, true, &amp;quot;&amp;quot;, &amp;quot;&amp;quot;, false, true))"&gt;Add Note&lt;/a&gt; </code></pre> <p>This works nicely in FireFox &amp; Chrome. However it only disabled the button in IE, and did not submit at all.</p> <p>To get the submission to work in IE I had to add</p> <pre><code>currentPage.ClientScript.GetPostBackEventReference(new PostBackOptions(button)) </code></pre> <p>Into the javascript, which makes it work as desired in IE, but now submits on every click in Firefox &amp; Chrome.</p> <p>Can anyone suggest a fix or an alternative?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload