asp.net add custom control in website

和自甴很熟 提交于 2019-12-07 12:42:53

问题


I want to make a custom control in my website (note: not web application)

Following is the code

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
    {

    }
}

Default.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>

When i compile the site, it gives me error for assembly.

Error = "The type or namespace name 'AnkitControls' does not exist in the namespace 'AnkitControls' (are you missing an assembly reference?)"


回答1:


Use the Register tag correctly:

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

The syntax you specify is for when your controls are in another assembly.




回答2:


You need to add a DLL project to your solution and reference it in your web project, or if you develop the DLL outside the web solution, simply add a reference to the compiled DLL. Next, you need to register the control in your web.config or at the page level. I do not recommend developing custom controls within a web site project.

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

Check out this website. http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx

If you're set on creating custom controls within a website project, the class does have to reside in the App_Code folder, but the registration isn't straight forward because Microsoft prepends the namespace with ASP. I had a very difficult time doing it this way so I created a DLL project.




回答3:


Have you checked that your custom component output binary is name AnkitControls.dll ? Did you added it as a reference in your website properties ?



来源:https://stackoverflow.com/questions/6903754/asp-net-add-custom-control-in-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!