asp.net – When and where does a UserControl compile – Education Career Blog

I have a page, Default.aspx, and a UserControl, HelloControl.ascx. In the page, I dynamically instantiate the control as follows:

protected void Page_Load(object sender, EventArgs e)
{
    HelloControl c = Page.LoadControl(typeof (HelloControl), null) as HelloControl;
    c.Greet();
}

This works fine, and the user control writes “Hello from a control” to the response. I have no @Register directive in Default.aspx, but when I try a similar dynamic control creation on a client’s machine, I get an error that the “Type or Namespace does not exist”.

I have even gotten feedback, on forums, from MS, that I need the @Register directive, but I obviously don’t. Can anyone help me out with info on how and when the user control is compiled if no @Register directive references it?

EDIT: I have tried a different direction of investigation, losing my initial call to LoadControl, and I still can’t reproduce the problem. The following code also works fine on my dev machine, without any @Register directive.

protected void Page_Load(object sender, EventArgs e)
{
    HelloControl c = new HelloControl();
    Response.Write(c.Greet());
}

,

use full path to control :

this.Controls.Add(Page.LoadControl("~/CustomControls/HelloControl.ascx"));

Leave a Comment