[英]c# asp.net , add custom control
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. 我已经在asp.net网络表单(c#)中创建了一个名为“ Article”的用户控件,其中包含文章的常规布局,并具有一个称为datasource的公共属性。 the datasource will be populated by a subsonic (DAL) activerecord.
数据源将由亚音速(DAL)活动记录填充。 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. 但是,我的问题是,当我尝试将控件添加到占位符时,所有我的控件(如标签,文字等)都会收到null引用异常。
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.
我每页只添加10个此类文章控件。
Thanks for your time! 谢谢你的时间!
Kind regards, Mark 亲切的问候,马克
You don't instantiate a user control. 您没有实例化用户控件。 You need to make use of the LoadControl method.
您需要使用LoadControl方法。
Like this: 像这样:
ArticleUserControl myControl = (ArticleUserControl)LoadControl("~/PathToUc/MyControl.ascx");
myControl.datasource = item;
phControl.Controls.Add(myControl);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.