繁体   English   中英

如果动态添加到Page.Load中,则UserControl不会出现在页面上

[英]UserControl doesn't appear on page if dynamically added in Page.Load

我有一个自定义ASP.NET用户控件,该控件在Page.Load方法中添加到网页的面板中。 该控件包含一些标签和一个GridView。 我使用以下代码将其添加到页面中:

ReportingControl rc = new ReportingControl(this.Username, this.SoaContext, transactionId, p.PaymentTypeRequestKey);
this.pnlPB.Controls.Add(rc);

由于某些原因,添加的控件实际上未显示在页面上。 我还注意到该控件内的所有控件均为空,但是我不知道这是否是问题的一部分,或者它们是否在以后创建。 有任何想法吗?

是从CompositeControlWebControl等继承的UserControl还是自定义服务器控件?

通常,对于用户控件,您可以通过ascx位置加载它们,例如:

//load the control
Control rc= LoadControl("~/UserControls/MyControl.ascx");

//set up values as needed
rc.Username = this.Username; 

//add the control where needed
this.pnlPB.Controls.Add(rc);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM