Friday, 19 September 2014

Disable Button Double Click to prevent multiple postbacks in ASP.NET using C#/VB.NET Or Prevent button double-click

Hi friends,in this article I will explain about How to Disable Button Double Click to prevent multiple postbacks in ASP.NET using C#/VB.NET  Or Prevent button double-click.
Most of the times we wish to disable the asp.net button control just after the first click so that we may prevent multiple postbacks.
This is simply to prevent users from clicking submit again while the form is processing. Mostly file uploads and credit card transactions.
Add following code to Page_load. This code prevents button double-click on client-side but still executes code at server-side:

ASP.NET:

<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="but_red" OnClick="btnSave_Click">

C#:
       
btnSave.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnSave, null) + ";");  

VB.NET:
      
 btnSave.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnSave, Nothing) + ";")


Explanation: The statement (actually multiple statements) disables the button, initiates a postback indicating the Click event of the button occured, and finally returns false just to make sure that a 2nd post is not attempted.



No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.