How to add user control dynamically without using <% @ register%> or <% @ Reference%>?

I have some usercontrol that loads dynamically. I know how to add these user controllers dynamically. The problem is that user control must be added, it is solved only at runtime. Therefore, I cannot use <% @ register%> or <% @ Reference%> on the .aspx page.

How to access usercontrols without using these directives?

+3
source share
6 answers

web.config, , , ( ). .

<pages>
  <controls>
    <add tagPrefix="myControl" src="~/Controls/uc.ascx" tagName="header"/>
 </controls>
</pages>

/

+2

, , , Page_Load:

var ctrl = LoadControl ( "yourcontrol.ascx" ); Controls.Add(Ctrl);

+1

( web.config), .

0

, , , .

, LoadControl:

void Page_Init(object sender, System.EventArgs e)
{
    // Instantiate the UserControl object
    MyControl myControl1 =
        (MyControl)LoadControl("TempControl_Samples1.ascx.cs");
    PlaceHolder1.Controls.Add(myControl1);
}

, , . - .

0

. webconfig.

0

code-behind:

var ctrl = LoadControl("user.ascx");
this.Controls.Add(ctrl);
0

All Articles