asp.net在网站中添加自定义控件

我想在我的网站上制作一个自定义控件(注意:不是Web应用程序)

以下是代码

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel; using System.Web.UI; using System.Web.UI.WebControls; namespace AnkitControls { ///  /// Summary description for CustomTreeView ///  public class CustomTreeViewControl : WebControl { } } 

Default.aspx:

      

Welcome to ASP.NET!

To learn more about ASP.NET visit www.asp.net.

You can also find documentation on ASP.NET at MSDN.

当我编译网站时,它给我组装错误。

Error =“名称空间’AnkitControls’中不存在类型或命名空间名称’AnkitControls’(您是否缺少程序集引用?)”

正确使用Register标记:

 <%@ Register TagPrefix="my" TagName="control" Src="~/Path/To/Your.ascx" %> 

您指定的语法适用于控件在另一个程序集中的时间。

您需要将DLL项目添加到您的解决方案并在您的Web项目中引用它,或者如果您在Web解决方案之外开发DLL,只需添加对已编译DLL的引用。 接下来,您需要在web.config或页面级别注册该控件。 我不建议在网站项目中开发自定义控件。

 <%@ Register TagPrefix="ControlVendor" Assembly="ControlVendor" %> 

看看这个网站。 http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx

如果您打算在网站项目中创建自定义控件,则该类必须驻留在App_Code文件夹中,但注册不是直接的,因为Microsoft使用ASP预先设置了命名空间。 我这样做非常困难所以我创建了一个DLL项目。

您是否检查过自定义组件输出二进制文件是否为AnkitControls.dll? 您是否在网站属性中添加了它作为参考?