Found this technique in a comment on this post and thought it warranted a repeat as it took a while for me to find it and I hadn’t seen this before (maybe I’m out of touch but humour me).

Most posts on this topic focus on disabling the submit button that causes the post. This works fine when you only have one submit control. But, if you have multiple buttons and/or link buttons that can cause a post and want to avoid potential conflicts if a user clicks a second submit control before the first one has been processed then this is really simply approach.

 

Assuming you can live with the fact that javascript must be enabled then all you need to do is to change your page from,

<form id="form1" runat="server" >  
    
    <!-- will cause a post, via a call to __doPostBack -->
    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Do Something</asp:LinkButton> 
    
    <!-- will cause a post via standard HTML <input type="submit" ... -->
    <asp:Button ID="Button1" runat="server" Text="Do Something Else" onclick="Button1_Click" /> 
 
</form>

to this,

<form id="form1" runat="server" onsubmit="this.onsubmit=function(){return false;}; return true;" >  
    
    <!-- will cause a post, via a call to __doPostBack -->
    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Do Something</asp:LinkButton> 
    
    <!-- will cause a post via standard HTML <input type="submit" ... -->
    <asp:Button ID="Button1" runat="server" Text="Do Something Else" onclick="Button1_Click" /> 
 
</form>

The onsubmit attribute is set to redefine itself once it has been executed on the first submit. Subsequent attempts to submit the form will be ignored as the redefined function simply returns false.

Once the form has been submitted for the first time and returns the original definition on the attribute is reinitialised and the form can be submitted again.

 

Nice and easy!

kick it on DotNetKicks.com