LoadControl vs Construct ASP.Net Control

后端 未结 3 820
Happy的楠姐
Happy的楠姐 2021-02-14 19:50

I have a question why we can only add dynamic control using LoadControl. For example:

public partial class wucReportParam : System.Web.UI.UserControl
{
    prote         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-02-14 20:11

    The initialization of the controls inside a User Control is driven by the ASCX file. Using only "new SomeControl" will not cause this initialization to run, and even if it did, all the design (markup) in the ascx file would be lost.

    Remember that the class "wucDate" is only the base class which the full user control inherits from. It's not the same class as you'll get when using LoadControl("wucDate.ascx").

    And to be honest, LoadControl has not much, if anything, to do with the page life cycle. That part is handled when you add the control to the container's Controls collection.

提交回复
热议问题