asp.net添加自定义控制在网站 [英] asp.net add custom control in website

查看:157
本文介绍了asp.net添加自定义控制在网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

以下是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的:

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.

错误=类型或命名空间名称AnkitControls没有命名空间AnkitControls存在(是否缺少程序集引用?)

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

推荐答案

您需要一个DLL项目添加到您的解决方案,并引用它在你的Web项目,或者如果你开发的Web解决方案外的DLL,只需添加一个参考到编译的DLL。接下来,你需要注册在你的web.config或在页面级别的控制。我不推荐一个网站项目中开发自定义的控件。

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" %>

看看这个网站。 <一href="http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx" rel="nofollow">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文件夹,但注册是不是直线前进,因为微软prepends与ASP的空间。我有一个非常困难的时期做这种方式,所以我创建了一个DLL项目。

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.

这篇关于asp.net添加自定义控制在网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆