views:

798

answers:

5

Hi Guys,

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

public partial class wucReportParam : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
          wucDate() ctrl = new wucDate();
          pnl.Controls.Add(ctrl);
    }
}

When in the page_load method of wucDate, the child control of wucDate is null but when i use the following method:

   public partial class wucReportParam : System.Web.UI.UserControl
    {
        public Report Report;

        protected void Page_Load(object sender, EventArgs e)
        {
              ctrl = (wucDate)LoadControl(@"Reports\wucDate.ascx");
              pnl.Controls.Add(ctrl);
        }
    }

In the page_load method of wucDate, the child control of wucDate is not null. Is anyone could explain to me why asp .net don't create any child control of wucDate when i using contructor ??? Thank you

+5  A: 

When dynamically loading a user control, it is important to ensure that the standard ASP.NET page event pipeline is initiated and progresses normally. When you use the new operator to create an instance of a user control, that user control is not properly added to ASP.NET's event system. If the events (Init, Load, PreRender, etc.) to not fire, then your control will never function properly. That is why it is necessary to use LoadControl, as that will make sure that the instance of your user control is created properly and attached to ASP.NET.

jrista
A: 

As I recall, it pertains to how ASP.NET constructs page components at run time. In ASP.NET although your pages have a class which is defined in your code-behind file, their types don't truly exist until run time. Like a page, although you have a control defined the wucDate type isn't created until it is included at run time. For this reason, the control must be loaded with LoadControl in order to both initialize the type and properly run in through the page life cycle.

This is to the best of my memory so if I'm incorrect here please let me know.

Nathan Taylor
+2  A: 

Apparently, using LoadControl with typeof (or GetType) has the same problem as using 'new' where the child controls are not initialized. Using LoadControl with a string to the ASCX file works.

Does not initialize child controls.

LoadControl(typeof(MyReport), null);

Works!

LoadControl("Report.ascx");
Doug D
There's an explanation as to why you're seeing this on the docs/connect site: http://msdn.microsoft.com/en-us/library/ewtd66a0.aspx (scroll to Community content) or https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=103456
Zhaph - Ben Duguid
+1  A: 

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.

gunteman
A: 

So what then is the purpose of

LoadControl(Type t, object[] parameters)

if it doesn't load controls properly? If we have to use

LoadControl(string virtualPath)

then why have the other option at all? I'm sure there is a valid use for it. Can someone please explain?

Justin