Tuesday, October 18, 2011

Using AJAX in a SharePoint webpart - UpdatePanel control

To take advantage of AJAX and partial page rendering in a SP2010 webpart, add your radio button, dropdown list, etc. controls to an UpdatePanel for a better user experience.

The CGCC-Parent.master file already contains the ScriptManager control so there is no need to add this to your webpart.

1. Create an instance of the panel. Set the UpdateMode to conditional.

updatePanel2 = new UpdatePanel();
updatePanel2.UpdateMode = UpdatePanelUpdateMode.Conditional;
2. Add your controls to the updatePanel. Make sure you add the controls to the ContentTemplateContainer.

updatePanel1.ContentTemplateContainer.Controls.Add(creditRadio);
updatePanel1.ContentTemplateContainer.Controls.Add(creditRequired);
updatePanel1.ContentTemplateContainer.Controls.Add(campusQ1);
updatePanel1.ContentTemplateContainer.Controls.Add(creditCheckBox);

3. Finally, and add your panel to the list of controls.
Controls.Add(updatePanel1);