如何让 ASP.NET 识别我的自定义服务器控件?

发布于 2024-08-07 13:47:22 字数 735 浏览 3 评论 0原文

我正在尝试创建我的第一个从面板派生的 ASP.net 服务器控件。由于某种原因,即使应用程序识别了我的控件的类和命名空间,我也无法让 .aspx 页面识别我的服务器标记。

以下是我使用的步骤:

1) 我创建了一个 CollapsablePanel 类,并将其放置在我的 site_code 目录中。我使用的是 Web 应用程序而不是网站,因此 App_Code 对我来说并不可用。

Namespace webstation.WebControls

    Public Class CollapsablePanel
        Inherits System.Web.UI.WebControls.Panel


    End Class

End Namespace

2)在.aspx文件中,我添加了 <%@ Register TagPrefix="webstation" Namespace="MyApplication.webstation.WebControls" %>

我已经构建了该项目,但我的自定义标签前缀没有出现。如果我继续输入,编辑器不会抛出错误,但是当我发布它并尝试访问它时,页面会抛出错误。如果我尝试从代码隐藏(Imports MyApplication.webstation.WebControls)访问该类,自定义控件会出现在智能感知中,因此我知道 Visual Studio 在某种程度上正在读取类信息。

我在这里做错了什么?任何想法都将不胜感激。

麦克风

I am trying to create my first ASP.net server control derived from a Panel. For some reason, I am unable to get the .aspx page to recognize my server tag even though the application recognizes the class and namespace of my control.

Here are the steps I've used:

1) I created a class CollapsablePanel that I've placed in my site_code directory. I am using a Web App not Web Site so App_Code is not really available to me.

Namespace webstation.WebControls

    Public Class CollapsablePanel
        Inherits System.Web.UI.WebControls.Panel


    End Class

End Namespace

2) In the .aspx file I've added <%@ Register TagPrefix="webstation" Namespace="MyApplication.webstation.WebControls" %>

I've built the project but my custom tag prefix does not appear. If I just go ahead and type the editor does not throw an error, however the page does when I publish it and try to access it. If I try to access the class from the codebehind (Imports MyApplication.webstation.WebControls) the custom control appears in intellisense, so I know that Visual Studio is reading the class information to some extent.

What am I doing wrong here? Any thoughts are greatly appreciated.

Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦开始←不甜 2024-08-14 13:47:22

看来您可能缺少 TagName 属性
一旦

<%@ Register TagPrefix="webstation" TagName="CollapsiblePanel" Namespace="MyApplication.webstation.WebControls" %>

你这样做了,你应该能够访问它

<webstations:CollapsiblePanel id='usercontrol1" runat="server" />

seems like you may be missing the TagName attribute
as

<%@ Register TagPrefix="webstation" TagName="CollapsiblePanel" Namespace="MyApplication.webstation.WebControls" %>

once you do this you should be able to access it as

<webstations:CollapsiblePanel id='usercontrol1" runat="server" />
江湖彼岸 2024-08-14 13:47:22

查看 Scott Gu 关于注册控件的博客文章,我喜欢自己在 web.config 文件中注册它们。
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 文件夹中。您还可以在 web.config 中注册用户控件,两个示例如下:

Check out Scott Gu's blog post on registering controls, I like registering them in the web.config file myself.
http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx

You need to make sure you have a fully qualified reference to the control class, meaning the library name and namespace. I place my controls in a class library, but you can include them in your App_Code folder. You can also register user controls in the web.config, both examples follow:

情魔剑神 2024-08-14 13:47:22

一旦我继续在同一解决方案中创建了一个类库项目,构建了该项目,从类库的 bin 中复制了 DLL,并将其放置在我的 Web 项目的文件夹中,我就能够像预期一样工作了保存外部二进制文件。我还在项目中添加了对 .dll 的引用。一旦我这样做了,语法:

<%@ Register Assembly="webstation.WebControls" Namespace="webstation.WebControls" TagPrefix="webstation" %>

就开始工作了。有谁知道我是否能够以某种方式从同一解决方案中的类库自动更新我的 Web 项目中已编译的 .DLL?或者我每次都必须进入 bin 文件夹并手动将其复制到 Web 项目中?

谢谢,

迈克

I was able to get it work like expected once I went ahead and created a Class Library project in the same solution, built the project, copied the DLL from the bin of the Class Library and placed it in a folder of my Web Project used for holding external binaries. I also added a reference to the .dll in the project. Once I did that, then the syntax:

<%@ Register Assembly="webstation.WebControls" Namespace="webstation.WebControls" TagPrefix="webstation" %>

began to work. Does anyone know if I am able to somehow automatically update the compiled .DLL in my web project from the class library in the same solution? Or do I just have to go into the bin folder each time and manually copy it into the web project?

Thanks,

Mike

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文