Programmatically creating a user control in a web application project

In the Website project , if I add a link to a user control on the page layout or control, like this:

<%@ Reference Control="~/controls/myUserControl.ascx" %>

then I can programmatically create an instance of this control from code using an automatically generated namespace ASP:

ASP.controls_myUserControl_ascx myControlInstance =
    new ASP.controls_myUserControl_ascx();

However , a compilation error occurs in the Web application project . This MSDN comment assumes that it simply does not work in a web application project, but does not offer an alternative.

I can create an instance of the class that exists behind the user control like this:

MyWebApplication.controls.myUserControl myControlInstance
    = new MyWebApplication.controls.myUserControl();

( ) .ascx, .

, :

MyWebApplication.controls.myUserControl myControlInstance
    = LoadControl("~/controls/myUserControl.ascx")
      as MyWebApplication.controls.myUserControl;

, , . , , , .

, : -? LoadControl?

+3
1

LoadControl - , .

0

All Articles