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?
web.config, , , ( ). .
web.config
<pages> <controls> <add tagPrefix="myControl" src="~/Controls/uc.ascx" tagName="header"/> </controls> </pages>
/
, , , Page_Load:
var ctrl = LoadControl ( "yourcontrol.ascx" ); Controls.Add(Ctrl);
( web.config), .
, , , .
, 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); }
, , . - .
. webconfig.
code-behind:
var ctrl = LoadControl("user.ascx"); this.Controls.Add(ctrl);