Monday, July 4, 2011

ASP.NET: Prevent button double-click

Problem

How to prevent button double-click on ASP.NET page

Solution

Add following code to Page_load. This code prevents button double-click on client-side but still executes code at server-side:

VB.Net

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

C#

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