UserControl doesn't appear on page if dynamically added in Page.Load
I have a custom ASP.NET user control that is added to a panel on a web page in the Page.Load method. The control contains some Labels and a GridView. I'm adding it to the page with this code:
ReportingControl rc = new ReportingControl(this.Username, this.SoaContext, transactionId, p.PaymentTypeRequestKey);
this.pnlPB.Controls.Add(rc);
For some reason, the controls that are added aren't actually showing up on the page. I also noticed that the controls within the control开发者_StackOverflow中文版 are all null but I don't know if that is part of the problem or if they are created at a later time. Any ideas?
Is it a UserControl
or a custom server control inheriting from CompositeControl
, WebControl
, etc.?
Generally with user controls, you load them via the ascx location, such as:
//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);
精彩评论