Disabling Form Buttons and Still Get the PostBack Event to Fire

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); 


Technorati Tags: ,,

About Geoff Varosky
Geoff Varosky is a Senior Architect for Insight, based out of Watertown, MA. He has been architecting and developing web based applications his entire career, and has been working with SharePoint for the past 15 years. Geoff is an active member of the SharePoint community, Co-Founder and Co-Organizer of the Boston Area SharePoint Users Group, co-founder for the Boston Office 365 Users Group, co-organizer for SharePoint Saturday Boston and speaks regularly at SharePoint events and user groups.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: