ASP.NET C# 中的自定义控件
我创建了一个简单的自定义控件,它仅继承自 Literal
控件,并且还没有任何扩展,代码为空。
命名空间: CustomControls
类名: Literal : System.Web.UI.WebControls.Literal
接下来我要做的就是在 aspx
页面中注册此控件,如下所示:(
<%@ Register TagPrefix="web" Namespace="CustomControls" %>
我在一些教程中读到这是其中之一注册它的方法,除了 web.config 等。)
毕竟,对我来说没有智能,更糟糕的是,当我尝试运行带有控件的页面时,我收到解析错误“未知的服务器标记:web”。
我使用“创建新项目”而不是新网站,以防需要此信息。
我的问题可能是什么?
提前致谢。
I created a simple custom control that only inherits from the Literal
control, and doesn't have any extensions yet, code is empty.
Namespace: CustomControls
Class name: Literal : System.Web.UI.WebControls.Literal
Next thing I do is registering this control in the aspx
page as following:
<%@ Register TagPrefix="web" Namespace="CustomControls" %>
(I read in few tutorials that this is one of the ways to register it, besides web.config etc.)
After all, no intellisence for me, and worse- I get a parse error 'unknown server tag: web' when I try to run the page with the control in it.
I used 'create new project' and not new website, in case this info is needed.
What could be my problem?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下是我的做法,从无到有,一步一步做起。第一种方法使用第二个项目/程序集。对于 App_code 版本,向下滚动。
Web应用程序项目方法
为类定义添加类似于以下内容的代码:
将以下寄存器标记添加到 aspx 页面的顶部
<%@ Register assembly="WebApplication2" namespace="CustomControls" tagprefix="web" %>
如果您的程序集名称不同,请在此处更改。我注意到,当我在 VB.Net 中执行此操作时,命名空间是 WebApplication1.CustomControls,而不是像 C# 中那样只是 CustomControls,有点奇怪。
将新控件添加到您的页面:
单独项目方法
向名为 Literal 的新项目添加一个新类(我使用的是 C#,因此我的文件名为 Literal.cs)。下面是我的超级基本代码,我相信它应该与问题中描述的代码相匹配。
将对 CustomControls 项目的引用添加到您的网站。
将程序集注册添加到 aspx 页面的顶部:
<%@ Register assembly="CustomControls" namespace="CustomControls" tagprefix="web" %>
将控件的新实例添加到页面:
在 App_Code 方法中
向 App_Code 文件夹 Literal2 添加一个新类(我使用的是 C#,因此我的文件名为 Literal2.cs)。下面是我的超级基本代码,我相信它应该与问题中描述的代码相匹配。我将其称为 2,以便您可以将其与上述方法结合使用而不会出现编译错误
通过将以下行添加到顶部,在您的 aspx 页面中注册 app_code 的程序集/命名空间
<%@ Register Namespace="CustomControls" Assembly="__code" tagprefix="web" %>
将新控件的实例添加到您的页面:
我使用 Visual Studio 对此进行了测试,一切对我来说都运行良好。
Here is how I did it, step by step starting from nothing. This first method uses a second project/assembly. For the App_code version scroll down.
Web Application Project Method
Put in code similar to the following for the class defenition:
Add the following register tag to the top of your aspx page
<%@ Register assembly="WebApplication2" namespace="CustomControls" tagprefix="web" %>
If your assembly name was different then change it here. I noticed that when I did this in VB.Net the namespace was WebApplication1.CustomControls instead of just CustomControls like it was in C#, kind of odd.
Add the new control to your page:
<web:Literal ID="Literal1" runat="server" Text="test" />
Seperate Project Method
Add a new Class to the new project called Literal (I'm using C# so my file is named Literal.cs). Below is my super basic code, that I believe should match the code described in the question.
Add a reference to the CustomControls project to your website.
Add the assembly registration to the top of your aspx page:
<%@ Register assembly="CustomControls" namespace="CustomControls" tagprefix="web" %>
Add a new instance of the control to your page:
<web:Literal ID="Literal1" runat="server" Text="test" />
In App_Code Method
Add a new Class to the App_Code folder Literal2 (I'm using C# so my file is named Literal2.cs). Below is my super basic code, that I believe should match the code described in the question. I called it 2 so that you can use this in conjunction with the method described above without getting compile errors
Register the assembly/namespace for app_code in your aspx page by adding the following line to the top
<%@ Register Namespace="CustomControls" Assembly="__code" tagprefix="web" %>
Add an instance of the new control to your page:
<web:Literal2 ID="literal2" runat="server" Text="test2" />
I tested this using visual studio and it all worked fine for me.
如果您想在其他页面上使用您的控件,您也可以在 web.config 中注册您的控件
对于网站,这将起作用
You can also register your control in web.config if you would like to use it on other pages
For web site this will work
问题出在您的
<%@ Register TagPrefix="web" Namespace="CustomControls" %>
标记中:它缺少程序集名称!它必须变为
<%@ Register TagPrefix="web" Namespace="CustomControls" Assembly="YourAssemblyName" %>
。如果您在查找类的程序集名称时遇到困难,这里有一些提示:ASP.App_Code
应该可以工作,否则请给我们一些有关项目布局的信息!
The problem lies in your
<%@ Register TagPrefix="web" Namespace="CustomControls" %>
tag: it misses the assembly name!It must become
<%@ Register TagPrefix="web" Namespace="CustomControls" Assembly="YourAssemblyName" %>
. If you have difficulties finding the assembly name for your class here are a few hints:ASP.App_Code
should workOtherwise please give us some information about project layout!
最简单的方法是将 Literal.ascx 拖放到页面上(如果该页面位于同一解决方案中)。如果该页面是另一个解决方案,您可以将控件添加到 VS 工具箱并拖放- 将控件拖放到页面上。
The easiest thing to do is to drag-and-drop the Literal.ascx on to the page (if the page is in the same solution) .If the page is another solution, you could add the control to VS toolbox and drag-and-drop the control on the page.
您忘记了 src 属性。
You forgot the src attribute.