如何让 ASP.NET 识别我的自定义服务器控件?
我正在尝试创建我的第一个从面板派生的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来您可能缺少 TagName 属性
一旦
你这样做了,你应该能够访问它
seems like you may be missing the TagName attribute
as
once you do this you should be able to access it as
查看 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:
一旦我继续在同一解决方案中创建了一个类库项目,构建了该项目,从类库的 bin 中复制了 DLL,并将其放置在我的 Web 项目的文件夹中,我就能够像预期一样工作了保存外部二进制文件。我还在项目中添加了对 .dll 的引用。一旦我这样做了,语法:
就开始工作了。有谁知道我是否能够以某种方式从同一解决方案中的类库自动更新我的 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:
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