简体   繁体   中英

asp.net add custom control in website

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?)"

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM