简体   繁体   中英

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 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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