Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在LoadControl调用中引用类类型?_C#_Asp.net_Loadcontrol - Fatal编程技术网

C# 如何在LoadControl调用中引用类类型?

C# 如何在LoadControl调用中引用类类型?,c#,asp.net,loadcontrol,C#,Asp.net,Loadcontrol,在这个动态加载用户控件的示例中,如何声明“TimeDisplay”?这可以在代码隐藏中完成,还是在ascx页面中完成?这是一本书中的一个例子,我想对于代码文件相对于其他文件的位置有一些假设 protected void Page_Load(object sender, EventArgs e) { TimeDisplay ctrl = (TimeDisplay)Page.LoadControl("TimeDisplay.ascx"); PlaceHolder1.Controls.Add(ct

在这个动态加载用户控件的示例中,如何声明“TimeDisplay”?这可以在代码隐藏中完成,还是在ascx页面中完成?这是一本书中的一个例子,我想对于代码文件相对于其他文件的位置有一些假设

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

您确实需要在aspx页面中添加对控件的引用:

<%@ Reference Control="~/Controls_Path/TimeDisplay.ascx" %>

要在标记中声明控件,需要在page指令或
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" %>


他想“动态”创建控件为什么需要在标记中声明标记。这可能不会导致错误,但您计划如何在标记中引用控件?