Irritating error trying to use CreateChildControls in a user control.
June 8, 2010
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”;
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”>