Home
Manage Your Code
Snippet: Disable the clicked button or the submit button/Prevent multiple clicks of a submit button (ASP.NET)
Title: Disable the clicked button or the submit button/Prevent multiple clicks of a submit button Language: ASP.NET
Description: Disable the submit button in ASP.NET. Disable the clicked button in ASP.NET. Prevent multiple clicks of a submit button in ASP.NET If you want to disable a button on your asp.net page when it's clicked but still want to fire its server side event: Views: 3509
Author: Arun Thomas Date Added: 9/10/2008
Copy Code  
btnSave.Attributes.Item("onclick") = "this.disabled=true;" & GetPostBackEventReference(btnSave).ToString

(or)

btnSave.Attributes.Add("onclick", "this.value='Please wait...';this.disabled=true;" & GetPostBackEventReference(Me.btnSave))

(or)

btnSave.Attributes.Item("onclick") = "if(ValidateEntries()==true){this.disabled=true;btnCancel.disabled=true;" & GetPostBackEventReference(btnSave).ToString & ";}"

(or for ASP.NET 2.0 and above)

btnSave.Attributes.Add("onclick", "this.disabled=true;" + ClientScript.GetPostBackEventReference(btnSave, "").ToString()) 
Usage
Place the above snippet in Page_Load 
(Do not place the code in Not IsPostBack)
Notes
If you disable a button in Javascript on your asp.net page when it's clicked the server side event will not work. Adding the above snippet will disable the button and fire the server side code.