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

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

我想在我的网站上做一个自定义控件(注意:不是 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>
    /// 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>

当我编译网站时,它给我错误提示。

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

最佳答案

正确使用Register标签:

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

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

关于c# - asp.net在网站中添加自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6903754/

相关文章:

c# - 优化大型 switch 语句

c# - 在 asp.net 中等待 GetByteArrayAsync 不返回

c# - 如何使用 jQuery 读取 session 值

c# - 百分比计算不起作用?

c# - 如何创建像这样的 C# delegate double callable `f(a + k * h)` ?

javascript - Web 应用程序根运算符和脚本标签

c# - Regex.IsMatch 导致灾难性的回溯

c# - 保留从 TextBox 到 SQL Server 的数据换行符

c# - 在 WPF 窗口中完全删除标题栏的上下文菜单

c# - 从代码后面带参数调用 javascript 函数