views:

53

answers:

1

Heya folks,

I've created a usercontrol in asp.net webforms (c#) called 'Article' which contains a general layout for an article and has a public property called datasource. the datasource will be populated by a subsonic (DAL) activerecord. In the controls pre-render event it will read the record and populates the labels. I think this is a flexible solution with easy maintenance.

My problem however is that when I try to add the control to a placeholder all my controls like labels, literals and the like receive a null reference exception.

I'm trying to spawn the controls as followed:

        foreach (DAL.Article item in coll)
        {
            p7.Controls.General.Article.Article article = new p7.Controls.General.Article.Article();
            article.datasource = item;
            phContent.Controls.Add(article);
        }

Is there something I've completely missed? It's driving me crazy heheh! Another question is do you guys know if this method will have a big impact on performance. I'm only adding 10 of those article controls per page.

Thanks for your time!

Kind regards, Mark

A: 

You don't instantiate a user control. You need to make use of the LoadControl method.

Like this:

ArticleUserControl myControl = (ArticleUserControl)LoadControl("~/PathToUc/MyControl.ascx");
myControl.datasource = item;
phControl.Controls.Add(myControl);
RPM1984
Thank you i was kinda confused, working good now!
Mark