Irritating error trying to use CreateChildControls in a user control.

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”>

Problem solved!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>