So, I have an existing app where I have a sort of "main page" that contains an updatePanel. Its ContentTemplate basically loads a half-dozen different usercontrols.
I just finished adding two lines so that, basically, it will load two different instances of the same control. (But I have a special property on each line that... via a public property on the control... allows me basically to pass in a single word that helps flex the control's behavior, so it will end up displaying different data. The way I handle that is by dynamically setting that word as a Parameter on an ObjectDataSource that's defined in that usercontrol. )
<uc:DynamicDocumentsUserControl runat="server" ID="ucDynamicDocumentsUserControl" ShowSeparatorAfterGrid="true" dynamicDocTitle="CloudSolutions" />
<uc:DynamicDocumentsUserControl runat="server" ID="ucDynamicDocumentsUserControl1" ShowSeparatorAfterGrid="true" dynamicDocTitle="Partners"/>
Like I said, that's working.
BUT I thought it would be REALLY HANDY if, instead of hard-coding lines for these two control instances (not to mention, having to add a NEW line, every time I want to add in a new instance of the control)... I could instead put a loop in this central page's Page_Init method... and build/add the controls to that UpdatePanel dynamically.
I removed my hard-coded lines(see above) and added code to the Page_Init:
UserControls.DynamicDocumentsUserControl ctl1 = new UserControls.DynamicDocumentsUserControl();
ctl1.ID = "ucDynamicDocumentsUserControl";
ctl1.DynamicDocTitle = "CloudSolutions";
ctl1.ShowSeperatorAfterGrid = true;
ctl1.Visible = false;
UserControls.DynamicDocumentsUserControl ctl2 = new UserControls.DynamicDocumentsUserControl();
ctl2.ID = "ucDynamicDocumentsUserControl1";
ctl2.DynamicDocTitle = "Partners";
ctl2.ShowSeperatorAfterGrid = true;
ctl2.Visible = false;
UserControlsUpdatePanel.ContentTemplateContainer.Controls.Add(ctl1);
UserControlsUpdatePanel.ContentTemplateContainer.Controls.Add(ctl2);
But once that first ContentTemplateContainer.Controls.Add command fires, we go over to the Page_Init method of the "child" UserControl. And here, we try building and assigning the SelectParameter to the ObjectDataSource that's coded into the control's .ascx file.
Parameter pm2 = new Parameter();
pm2.Name = "documentTitle";
pm2.DefaultValue = dynamicdoctitle;
DynamicDocumentsDataSource.SelectParameters.Add(pm2);
And I get an error "Object reference is not set to an instance of an object. DynamicDocumentsDataSource was null."
It's as if the code-behind doesn't "see" the ?? Just for completeness-sake, here's that:
<asp:ObjectDataSource ID="DynamicDocumentsDataSource" runat="server"
TypeName="ConnectIT.Bll.Search" SelectMethod="GetPagedDataSet"
EnablePaging="true" SelectCountMethod="GetRowCount"
StartRowIndexParameterName="startRow"
MaximumRowsParameterName="pageSize" SortParameterName="sortColumns"
OnObjectCreated="dynamicDocuments_ObjectCreated">
<SelectParameters>
<asp:Parameter Name="storedProc" DefaultValue="[uspGetDynamicDocuments]" />
</SelectParameters>
</asp:ObjectDataSource>
I don't get it. Why is that part suddenly broken? It's as if the UserControl doesn't "see" this code or maybe the definition in the designer file. ???
I was able to get this working by loading in each of the two UserControls by file reference instead:
This worked just fine.