我有一个自定义 ASP.NET 用户控件,已通过 Page.Load 方法添加到网页上的面板中。该控件包含一些标签和一个 GridView。我使用以下代码将其添加到页面:
ReportingControl rc = new ReportingControl(this.Username, this.SoaContext, transactionId, p.PaymentTypeRequestKey);
this.pnlPB.Controls.Add(rc);
由于某种原因,添加的控件实际上并未显示在页面上。我还注意到控件中的控件都是空的,但我不知道这是否是问题的一部分,或者它们是否是稍后创建的。有什么想法吗?
最佳答案
它是UserControl
还是继承自CompositeControl
、WebControl
等的自定义服务器控件?
通常对于用户控件,您可以通过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);
关于c# - 如果在 Page.Load 中动态添加,则 UserControl 不会出现在页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3215393/