Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.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# 无法使用我的自定义服务器控件_C#_Asp.net_Web Config - Fatal编程技术网

C# 无法使用我的自定义服务器控件

C# 无法使用我的自定义服务器控件,c#,asp.net,web-config,C#,Asp.net,Web Config,我有一个包含两个项目的web应用程序:网站本身和一个包含自定义服务器控件的项目 我在web应用中引用了服务器控件项目,并将其添加到我的web.config中: <pages> <controls> <add assembly="ServerControls" tagName="MyControl" tagPrefix="sc" namespace="ServerControls" /> </controls> </

我有一个包含两个项目的web应用程序:网站本身和一个包含自定义服务器控件的项目

我在web应用中引用了服务器控件项目,并将其添加到我的web.config中:

<pages>
  <controls>
    <add assembly="ServerControls" tagName="MyControl" tagPrefix="sc" namespace="ServerControls" />
  </controls>      
</pages>


但是我如何使用控件呢?它没有显示在工具箱或intellisense中,当我在web.config中启动它时,它给了我一个错误。

您需要注册标记名,然后使用标记:

<%@ Register TagPrefix="sc" TagName="[control]" 
    Src="~/Controls/control.ascx" %>

在页面顶部,然后使用如下控件:

<sc:Control id="id" runat="server" />
<sc:MyControl runat="server" />

首先,您需要检查自定义控件类是否标记为public,否则从程序集外部看不到它

然后在页面中引用它,如下所示:

<sc:Control id="id" runat="server" />
<sc:MyControl runat="server" />


页面上不应该需要Register指令,因为您已经在web.config中注册了标记前缀。

我们谈论的是控件类(.cs或.vb)还是用户控件(.ascx)?