0

In this example for loading a user control dynamically, how do I declare "TimeDisplay"? Can this be done in the code behind, or is it done in the ascx page? This is an example from a book, I guess there are assumptions made as to where the code files are located relative to one another?

protected void Page_Load(object sender, EventArgs e) 
{ TimeDisplay ctrl = (TimeDisplay)Page.LoadControl("TimeDisplay.ascx"); 
PlaceHolder1.Controls.Add(ctrl); 
}
4

2 に答える 2

1

aspx ページにコントロールへの参照を追加する必要があります。

<%@ Reference Control="~/Controls_Path/TimeDisplay.ascx" %>
于 2011-10-19T18:53:40.833 に答える
0

マークアップでコントロールを宣言するには、ページ ディレクティブまたはweb.config. アプリケーションのどこでもコントロールを使用できるため、コントロールを に登録するweb.configことをお勧めします。

構成方法:

<pages>
    <controls>
        <add tagPrefix="uc1" src="~/controls/myusercontrol.ascx" tagName="myusercontrol" />
    </controls>
</pages>            

ページ ディレクティブ メソッド:

<%@ Register TagPrefix="uc1" TagName="MyUserControl" Src="~/controls/myusercontrol.ascx" %>
于 2011-10-19T19:02:41.583 に答える