【问题标题】:asp.net add custom control in websiteasp.net 在网站中添加自定义控件
【发布时间】:2011-08-02 20:23:42
【问题描述】:

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

下面是代码

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>
    /// Summary description for CustomTreeView
    /// </summary>
    public class CustomTreeViewControl : WebControl
    {

    }
}

默认.aspx:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <%@ Register Assembly="AnkitControls" Namespace="AnkitControls" TagPrefix="CustomCtrl" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
</asp:Content>

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

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

【问题讨论】:

  • 您的 web.config 中是否引用了 AnkitControls
  • CustomTreeViewControl.cs文件是放在本站App_Code文件夹还是其他项目中?
  • 它在网站的 CustomControls 文件夹中,而不是在 App_Code 中

标签: c# asp.net .net-4.0 custom-controls


【解决方案1】:

正确使用Register标签:

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

您指定的语法适用于您的控件位于另一个程序集中的情况。

【讨论】:

    【解决方案2】:

    您需要将 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 项目。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 这是一个我放代码的网站。它没有添加dll
      • 好的,我认为它是在一个单独的项目中用于重用目的
      猜你喜欢
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 2010-12-08
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 2011-04-16
      相关资源
      最近更新 更多