Disabling Form Buttons and Still Get the PostBack Event to Fire
December 18, 2008 Leave a comment
I was working on developing a web part today for a client to automatically provision SharePoint sites. One item I had left to take care of was disabling of the button after the form has been submitted, so that everything runs smoothly. Simple? Should be… but, but I was running into a wall.
Finally, I came across the answer – and I wish I still had it bookmarked, sorry for not passing along the credit on this one, but, it lies in creating a function that will allow the button to actually run the submit function, however, any clicks will return a false, allowing the button to basically be disabled from any further interaction. The code snippet is below (to be placed in the CreateChildControls section in the web part) with the aforementioned code to disable the button highlighted in bold.
1: btnCreate = new Button();
2: btnCreate.Text = "Create Site";
3: btnCreate.Click += new EventHandler(btnCreate_Click);
4: btnCreate.Attributes.Add("onclick","this.onclick=new Function('return false;');");
5: this.Controls.Add(this.btnCreate);