Opsgility Cloud Readiness Blog

The cloud moves fast! Keep up with the latest products, trends and more with Opsgility's industry experts and cloud-pros.

Irritating error trying to use CreateChildControls in a user control.

Posted on: June 8, 2010 by Michael Washam

I was trying to programmatically add an ObjectDataSource to my CreateChildControls method:

ObjectDataSource ods = new ObjectDataSource();
ods.SelectMethod = “GetDataTable”;
ods.TypeName = “SPWebParts.LeadViewer.LeadViewerUserControl”;
ods.ID = “leadsDS”;
Controls.Add(ods);

This SHOULD work! However, everytime I was getting the error:

“The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.”

 No idea why.. However, the quick solution is just to instantiate the ODS declaratively instead of CreateChildControls.

 <asp:ObjectDataSource ID=”leadsDS” runat=”server” SelectMethod=”GetDataTable” TypeName=”SPWebParts.LeadViewer.LeadViewerUserControl”>
</asp:ObjectDataSource>

Problem solved!

leave a reply

Connect with Opsgility